File size: 6,218 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | function outstring = round_timestring(timestring,timeunit)
% round_timestring - Rounds an input time and date to a specified accuracy
% (ms, s, m, h, or d) and returns a string to this accuracy
%
% Syntax: outstring = round_timestring(timestring,timeunit);
%
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Input:
%
% timestring - Input string of unrounded time/date information
% timeunit (optional) - Unit of time to which to round (if ommitted, ms
% is used by default)
%
% =========================================================================
%
% Output:
%
% outstring - Rounded output time/date string
%
% =========================================================================
%
% Initial version: Jul 2020; Latest update: Jun 2023
%
% ----------------- BEGIN CODE -----------------
persistent pathsAdded
if isempty(pathsAdded)
[p,~,~] = fileparts(mfilename('fullpath'));
s = what(fullfile(p, '../LoggingAndStringReporting')); addpath(s.path);
pathsAdded = true;
end
% Round a standard timestring (YYYY-MM-DD[T]HH:MM:SS.SSSSSSS)
% to a nearest time unit
% Initialize time unit
if nargin < 2
timeunit = 'ms'; % Default = milliseconds
else
timeunit = lower(timeunit);
end
% Decompose time string
copy = timestring;
copy = strrep(copy,':',' ');
copy = strrep(copy,'T',' ');
[parts, Nparts] = string_parts(copy);
if Nparts < 4 || length(parts{1}) ~= 10
error('round_timestring:InvalidInputFormat', 'Invalid YYYY-MM-DD[T]HH:MM:SS.SSSSSSS format');
end
dt_st = parts{1};
hr_st = parts{2};
mn_st = parts{3};
sc_st = parts{4};
sc = str2double(sc_st);
mn = str2double(mn_st);
hr = str2double(hr_st);
if (sc < 0) || (sc >= 60) || ...
(mn < 0) || (mn >= 60) || (rem(mn,1) ~= 0) || ...
(hr < 0) || (hr > 24) || (rem(hr,1) ~= 0) || ...
isnan(sc) || isnan(mn) || isnan(hr)
error('round_timestring:InvalidTimeFormat', 'Invalid HH:MM:SS.SSSSSS format');
end
switch timeunit
case {'ms','msec'}
% Round to ms
sc_st = num2str(sc, '%06.3f');
sc = str2double(sc_st);
dateIn = datenum(dt_st, 'yyyy-mm-dd');
if (sc >= 60)
mn = round(str2double(mn_st)+sc/60);
sc = 0;
end
if (mn >= 60)
hr = round(str2double(hr_st)+mn/60);
mn = 0;
end
if (hr >= 24)
dateIn = round(dateIn+hr/24);
hr = 0;
end
sc_st = num2str(sc, '%06.3f');
mn_st = num2str(mn, '%02.0f');
hr_st = num2str(hr, '%02.0f');
dt_st = datestr(dateIn, 'yyyy-mm-dd');
outstring = [dt_st ' ' hr_st ':' mn_st ':' sc_st];
case {'s','sec'}
% Round to s
sc_st = num2str(sc, '%02.0f'); % round
sc = str2double(sc_st);
dateIn = datenum(dt_st, 'yyyy-mm-dd');
if (sc >= 60)
mn = round(str2double(mn_st)+sc/60);
sc = 0;
end
if (mn >= 60)
hr = round(str2double(hr_st)+mn/60);
mn = 0;
end
if (hr >= 24)
dateIn = round(dateIn+hr/24);
hr = 0;
end
sc_st = num2str(sc, '%02.0f');
mn_st = num2str(mn, '%02.0f');
hr_st = num2str(hr, '%02.0f');
dt_st = datestr(dateIn, 'yyyy-mm-dd');
outstring = [dt_st ' ' hr_st ':' mn_st ':' sc_st];
case {'m','min'}
% Round to m
if (sc >= 30)
mn = mn+1;
end
dateIn = datenum(dt_st, 'yyyy-mm-dd');
if (mn >= 60)
hr = round(str2double(hr_st)+mn/60);
mn = 0;
end
if (hr >= 24)
dateIn = round(dateIn+hr/24);
hr = 0;
end
mn_st = num2str(mn, '%02.0f');
hr_st = num2str(hr, '%02.0f');
dt_st = datestr(dateIn, 'yyyy-mm-dd');
outstring = [dt_st ' ' hr_st ':' mn_st];
case {'h', 'hr', 'hour'}
% Round to hr
if (mn >= 30)
hr = hr+1;
end
dateIn = datenum(dt_st, 'yyyy-mm-dd');
if (hr >= 24)
dateIn = round(dateIn+hr/24);
hr = 0;
end
mn_st = '00';
hr_st = num2str(hr, '%02.0f');
dt_st = datestr(dateIn, 'yyyy-mm-dd');
outstring = [dt_st ' ' hr_st ':' mn_st];
case {'d', 'dy', 'day'}
% Round to dy
if (hr >= 12)
dateIn = datenum(dt_st , 'yyyy-mm-dd');
dt_st = datestr(round(dateIn+hr/24), 'yyyy-mm-dd');
end
outstring = dt_st;
otherwise
fprintf('*** Error -- Invalid time unit: %s (pick from: ms, msec, s, sec, m, min, h, hr, hour, d, dy, day)\n', timeunit);
error('round_timestring:InvalidTimeUnit', 'Invalid time unit');
end
return
end
% ----------------- END OF CODE -----------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% D. Hall | 2020-Jul-29 | Created
% E. White | 2023-Jun-07 | Numerous bug fixes, header and footer
% E. White | 2023-Jun-13 | Removed commented-out code block
% =========================================================================
%
% Copyright (c) 2023 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
|