File Exchange

image thumbnail

timestr

version 1.5.0.1 (1.84 KB) by Michelle Hirsch
String representation of time in HH:MM:SS.SSSS format.

1 Download

Updated 01 Sep 2016

View License

Convert serial date numbers into time strings. Time strings are of HH:MM:SS.SSS format - like date strings, but without the date.
Ex:
timestr(now)
09:15:36.4000
Note: A time datetime object was introduced in MATLAB R2014b which is way better than this little utility. If you've got access to it, use it instead!

Comments and Ratings (4)

A faster version, which correctly handles rounding and zero decimal digits:

function str = timestr(dtn,prc)
% Return a string representation of a serial date number: 'HH:MM:SS.SSSS'.
%
% (c) 2014 Stephen Cobeldick
%
% Syntax:
% str = timestr
% str = timestr(dtn)
% str = timestr(dtn,prc)
%
% Convert the time portion of a serial date number into a string, formatted
% as 'HH:MM:SS.SSSS', with optional control over the seconds' precision.
% Input <dtn> can be a scalar, or an array of any dimensions: the output
% <str> is a char array whose rows are linear indexed from <dtn>.
%
% This is a drop-in replacement for Michelle Hirsch's "timestr": it fixes
% some rounding issues, allows zero decimal digits, and is faster!
%
% See Also DATESTR DATENUM NOW CLOCK DATESTR8601 DATENUM8601 DATEROUND ROUND2DP ROUND2SF
%
% ### Examples ###
%
% Examples use the date+time described by the vector [1999,1,3,15,6,48.0568].
%
% timestr
% ans = '15:06:48.0568'
%
% timestr(730123.62972287962)
% ans = '15:06:48.0568'
%
% timestr(730123.62972287962,2)
% ans = '15:06:48.05'
%
% timestr(730123.62972287962,0)
% ans = '15:06:48'
%
% timestr([730123.62972287962,0.62972287962],3)
% ans = ['15:06:48.056';'15:06:48.056']
%
% ### Input and Output Arguments ###
%
% Inputs (*=default):
% dtn = NumericArray, the SerialDateNumbers to convert, any size not empty, *now.
% prc = NumericScalar, the number of decimal digits of the seconds, *4.
%
% Output:
% str = CharArray, with numel(dtn) rows. Each row is linear indexed from <dtn>.
%
% str = timestr(*dtn,*prc)

if nargin<1
dtn = now;
else
assert(isnumeric(dtn)&&isreal(dtn),'First input must be a real numeric.')
end
if nargin<2
prc = 4;
else
assert(isnumeric(prc)&&isscalar(prc),'Second input must be a scalar numeric.')
end
%
[~,~,~,H,M,S] = datevecmx(dtn(:).');
fmt = sprintf('%%02d:%%02d:%%0%d.%df',prc+2+(prc>0),prc);
str = reshape(sprintf(fmt,[H;M;S-rem(S,10^-prc)]).',[],numel(dtn)).';
%
end
%----------------------------------------------------------------------END:timestr

Minor bug fix for my suggested code below: the logical compare 'prc>0' needs to be in brackets '(prc>0)'.

A useful concept but there are multiple mlint messages that need attention, and the author themselves makes one comment that things are "a bit messed up": no array preallocation, no logical indexing, unused variables, for-loops instead of vectorized code, vague input/output specifications, many commented-out lines of code... keep an eye on the ball, MATLAB!

Apart from tidying up the code, some design issues allow edge-case bugs to creep in too: the seconds' fractional part is rounded inside an "sprintf" call, which can result in some unexpected time values in the string:
timestr(datenum([1999,12,31,23,59,59.999]),2)
-> ans = '23:59:60.00'
timestr(datenum([1999,12,31,23,59,59.5]),0)
-> ans = ' 23:59:060'

The following code fixes this behavior by truncating at the required precision (consistent with how we increment time units only after they are complete), as well as being faster to compute:

%%% begin mfile code %%%
function str = timestrX(num,prc)
% Return a string representation of a serial date number: 'HH:MM:SS.SSSS'.
% num = NumericArray, the SerialDateNumbers to convert, any size not empty.
% prc = NumericScalar, the number of decimal digits of the seconds, default=4.
% str = CharArray, with numel(num) rows. Each row is linear indexed from num.
if nargin<2, prc = 4; end
[~,~,~,H,M,S] = datevecmx(num(:).');
fmt = sprintf('%%02d:%%02d:%%0%d.%df',prc+2+prc>0,prc);
str = reshape(sprintf(fmt,[H;M;S-rem(S,10^-prc)]).',[],numel(num)).';
end
%%% end mfile code %%%

timestrX(datenum([1999,12,31,23,59,59.999]),2)
-> ans = '23:59:59.99'
timestrX(datenum([1999,12,31,23,59,59.5]),0)
-> ans = ' 23:59:59'

Shi Shu

Updates

1.5.0.1

Updated license

1.5.0.0

Updated to work with R2014b. Make available as a Toolbox file.

1.2.0.0

Removed mistaken attribution.

1.1.0.0

Updated copyright.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux