-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time): add hour, minute, second time extraction functions (#1305)
- Loading branch information
1 parent
c648d20
commit 861ec8a
Showing
13 changed files
with
534 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
function varargout = hour(varargin) | ||
% Extracts the hour component from a given date/time input. | ||
% | ||
% Usage: | ||
% h = hour(d) | ||
% h = hour(d, formatDate) | ||
% | ||
% Input: | ||
% d - Date/time input (can be a string, char, or numeric). | ||
% formatDate - format date (can be a string, char). | ||
% | ||
% Output: | ||
% h - Hour component of the input date/time. | ||
|
||
narginchk(1, 2); | ||
nargoutchk(0, 1); % Ensure at most one output argument. | ||
|
||
d = varargin{1}; | ||
d = convertStringsToChars(d); % Convert strings to char for consistency. | ||
|
||
formatDate = ''; | ||
if nargin == 2 | ||
formatDate = convertStringsToChars(varargin{2}); | ||
end | ||
|
||
% Validate input type | ||
if ~(ischar(d) || isstring(d) || isnumeric(d) || iscell(d)) | ||
error('Invalid date/time format. Input must be a string, char, or numeric.'); | ||
end | ||
% Convert char inputs to numeric using datenum | ||
if ~isnumeric(d) | ||
d = datenum(d, formatDate); | ||
end | ||
|
||
% Convert numeric or parsed inputs to date vector | ||
c = datevec(d(:)); | ||
|
||
% Extract hour component | ||
h = c(:, 4); | ||
|
||
if ~ischar(d) | ||
h = reshape(h, size(d)); % Reshape to match the input size if numeric. | ||
end | ||
|
||
varargout{1} = h; % Assign output | ||
end | ||
%============================================================================= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
function varargout = minute(varargin) | ||
% Extracts the minute component from a given date/time input. | ||
% | ||
% Usage: | ||
% m = minute(d) | ||
% m = minute(d, formatDate) | ||
% | ||
% Input: | ||
% d - Date/time input (can be a string, char, or numeric). | ||
% formatDate - format date (can be a string, char). | ||
% | ||
% Output: | ||
% m - Minute component of the input date/time. | ||
|
||
narginchk(1, 2); | ||
nargoutchk(0, 1); | ||
|
||
inputDate = varargin{1}; | ||
inputDate = convertStringsToChars(inputDate); | ||
|
||
dateFormat = ''; | ||
if nargin == 2 | ||
dateFormat = convertStringsToChars(varargin{2}); | ||
end | ||
|
||
if ischar(inputDate) | ||
inputDate = datenum(inputDate, dateFormat); | ||
inputSize = size(inputDate); | ||
elseif iscell(inputDate) | ||
inputSize = size(inputDate); | ||
inputDate = datenum(inputDate, dateFormat); | ||
elseif isnumeric(inputDate) | ||
inputSize = size(inputDate); | ||
else | ||
error(_('Invalid date/time format. Input must be a string, char, or numeric.')); | ||
end | ||
|
||
dateVector = datevec(inputDate(:)); | ||
minuteValues = dateVector(:, 5); | ||
|
||
requiresMinuteIncrement = (dateVector(:, 6) > 59.5); | ||
minuteValues(requiresMinuteIncrement) = minuteValues(requiresMinuteIncrement) + 1; | ||
|
||
varargout{1} = reshape(minuteValues, inputSize); | ||
end | ||
%============================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
%============================================================================= | ||
% Copyright (c) 2016-present Allan CORNET (Nelson) | ||
%============================================================================= | ||
% This file is part of the Nelson. | ||
%============================================================================= | ||
% LICENCE_BLOCK_BEGIN | ||
% SPDX-License-Identifier: LGPL-3.0-or-later | ||
% LICENCE_BLOCK_END | ||
%============================================================================= | ||
function varargout = second(varargin) | ||
% Extracts the second component from a given date/time input. | ||
% | ||
% Usage: | ||
% s = second(d) | ||
% s = second(d, formatDate) | ||
% | ||
% Input: | ||
% d - Date/time input (can be a string, char, or numeric). | ||
% formatDate - format date (can be a string, char). | ||
% | ||
% Output: | ||
% s - Second component of the input date/time. | ||
|
||
narginchk(1, 2); | ||
nargoutchk(0, 1); | ||
|
||
inputDate = varargin{1}; | ||
inputDate = convertStringsToChars(inputDate); | ||
|
||
dateFormat = ''; | ||
if nargin == 2 | ||
dateFormat = convertStringsToChars(varargin{2}); | ||
end | ||
|
||
if ischar(inputDate) | ||
inputDate = datenum(inputDate, dateFormat); | ||
inputSize = size(inputDate); | ||
elseif iscell(inputDate) | ||
inputSize = size(inputDate); | ||
inputDate = datenum(inputDate, dateFormat); | ||
elseif isnumeric(inputDate) | ||
inputSize = size(inputDate); | ||
else | ||
error(_('Invalid date/time format. Input must be a string, char, or numeric.')); | ||
end | ||
|
||
dateVector = datevec(inputDate(:)); | ||
secondValues = dateVector(:, 6); | ||
|
||
secondValues = round(1000 .* secondValues) ./ 1000; | ||
varargout{1} = reshape(secondValues, inputSize); | ||
end | ||
%============================================================================= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xmldoc> | ||
<copyright>SAME AS NELSON SOFTWARE</copyright> | ||
|
||
<language>en_US</language> | ||
<keyword>hour</keyword> | ||
<short_description>Hours part of the input date and time.</short_description> | ||
|
||
<syntax> | ||
<syntax_item>h = hour(t)</syntax_item> | ||
<syntax_item>h = hour(t, formatIn)</syntax_item> | ||
</syntax> | ||
|
||
<param_input> | ||
<param_input_item> | ||
<param_name>t</param_name> | ||
<param_description>serial date number or text inputs</param_description> | ||
</param_input_item> | ||
<param_input_item> | ||
<param_name>formatIn</param_name> | ||
<param_description>valid date format</param_description> | ||
</param_input_item> | ||
</param_input> | ||
|
||
|
||
<param_output> | ||
<param_output_item> | ||
<param_name>h</param_name> | ||
<param_description>a double: integer value.</param_description> | ||
</param_output_item> | ||
</param_output> | ||
|
||
<description> | ||
<p><b | ||
>h = hour(t)</b> extracts the hour component from each date and time specified in <b | ||
>t</b>.</p> | ||
<p>The output <b | ||
>h</b> is a double array containing integer values ranging from 0 to 23.</p> | ||
</description> | ||
<used_function /> | ||
<bibliography /> | ||
|
||
<examples> | ||
<example_item> | ||
<example_item_type>nelson</example_item_type> | ||
<example_item_description /> | ||
<example_item_data | ||
><![CDATA[h = hour(738427.656845093) | ||
h = hour("2021/09/28 15:45:51", 'YYYY/M/DD HH:MM:SS') | ||
]]> | ||
</example_item_data> | ||
</example_item> | ||
|
||
</examples> | ||
|
||
<see_also> | ||
<see_also_item> | ||
<link linkend="${time}minute">minute</link> | ||
</see_also_item> | ||
<see_also_item> | ||
<link linkend="${time}second">second</link> | ||
</see_also_item> | ||
|
||
</see_also> | ||
|
||
<history> | ||
<history_item> | ||
<history_version>1.10.0</history_version> | ||
<history_description>initial version</history_description> | ||
</history_item> | ||
</history> | ||
|
||
<authors> | ||
<author_item>Allan CORNET</author_item> | ||
</authors> | ||
</xmldoc> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xmldoc> | ||
<copyright>SAME AS NELSON SOFTWARE</copyright> | ||
|
||
<language>en_US</language> | ||
<keyword>minute</keyword> | ||
<short_description | ||
>Minutes part of the input date and time.</short_description> | ||
|
||
<syntax> | ||
<syntax_item>m = minute(t)</syntax_item> | ||
<syntax_item>m = minute(t, formatIn)</syntax_item> | ||
</syntax> | ||
|
||
<param_input> | ||
<param_input_item> | ||
<param_name>t</param_name> | ||
<param_description>serial date number or text inputs</param_description> | ||
</param_input_item> | ||
<param_input_item> | ||
<param_name>formatIn</param_name> | ||
<param_description>valid date format</param_description> | ||
</param_input_item> | ||
</param_input> | ||
|
||
|
||
<param_output> | ||
<param_output_item> | ||
<param_name>m</param_name> | ||
<param_description>a double: integer value.</param_description> | ||
</param_output_item> | ||
</param_output> | ||
|
||
<description> | ||
<p><b | ||
>m = minute(t)</b> extracts the minute component from each date and time specified in <b | ||
>t</b>.</p> | ||
<p>The output <b | ||
>m</b> is a double array containing integer values ranging from 0 to 59.</p> | ||
</description> | ||
<used_function /> | ||
<bibliography /> | ||
|
||
<examples> | ||
<example_item> | ||
<example_item_type>nelson</example_item_type> | ||
<example_item_description /> | ||
<example_item_data | ||
><![CDATA[m = minute(738427.656845093) | ||
m = minute("2021/09/28 15:45:51", 'YYYY/M/DD HH:MM:SS') | ||
]]> | ||
</example_item_data> | ||
</example_item> | ||
|
||
</examples> | ||
|
||
<see_also> | ||
<see_also_item> | ||
<link linkend="${time}hour">hour</link> | ||
</see_also_item> | ||
<see_also_item> | ||
<link linkend="${time}second">second</link> | ||
</see_also_item> | ||
|
||
</see_also> | ||
|
||
<history> | ||
<history_item> | ||
<history_version>1.10.0</history_version> | ||
<history_description>initial version</history_description> | ||
</history_item> | ||
</history> | ||
|
||
<authors> | ||
<author_item>Allan CORNET</author_item> | ||
</authors> | ||
</xmldoc> |
Oops, something went wrong.