Clear Filters
Clear Filters

time in seconds to standard time

9 views (last 30 days)
Matthew Tourtelot
Matthew Tourtelot on 10 May 2017
Commented: Matthew Tourtelot on 10 May 2017
I have a function to convert time from a counter to the format "YYYY DDD\HH:MM:SS.000.000.000"
The function I use is very basic and takes up a lot of time. Can anyone help me to optimize the function below?
time64_nano=double(mod(time64,50000000)*20);
nano = floor(mod(time64_nano,1000));
micro = floor(mod(time64_nano,1000000)/1000);
milli = floor(time64_nano/1000000);
time64_seconds = double(time64)/50000000;
dayclock = mod(double(time64_seconds),(24*60*60));
dayno = floor(double(time64_seconds)/(24*60*60));
second = floor(mod(dayclock,60));
minute = floor(mod(dayclock,3600)/60);
hour = floor(dayclock/3600);
timestring = [repmat(num2str(year),size(time64,1),1) repmat(' ',size(time64,1),1)...
num2str(dayno,'%02d') repmat('\',size(time64,1),1)...
num2str(hour,'%02d') repmat(':',size(time64,1),1)...
num2str(minute,'%02d') repmat(':',size(time64,1),1)...
num2str(second,'%02d') repmat('.',size(time64,1),1)...
num2str(milli,'%03d') repmat('.',size(time64,1),1)...
num2str(micro,'%03d') repmat('.',size(time64,1),1)...
num2str(nano,'%03d')];
timestring=cellstr(timestring);

Answers (2)

Steven Lord
Steven Lord on 10 May 2017
Use the datetime function with the 'ConvertFrom' option. If your time in seconds is since 1-Jan-1970 00:00:00 UTC, use 'ConvertFrom' with 'posixtime'. If it's a number of seconds since some other date and time, use 'ConvertFrom' with 'epochtime', 'Epoch', and the date and time that is your baseline.
  2 Comments
Matthew Tourtelot
Matthew Tourtelot on 10 May 2017
Can datetime format it to the same format as above, and down to the nanosecond?
Steven Lord
Steven Lord on 10 May 2017
See the description of the datetime object's Format property for the list of "time components" you can use. See in particular the S identifier in the last row of that table.

Sign in to comment.


Peter Perkins
Peter Perkins on 10 May 2017
You don't say what the counter is: where it starts, what it counts, and how long it counts for.
Based on the code, it looks like it counts 20ns ticks from the start of the (a specific?) year. That's not a time system I'm familiar with, what is it? It's not NTP, or .NET or NTFS. Your code uses a var named time64, so I'm guessing that's a uint64. If you could use a double, the code would be simpler but you'd be limited to
>> 2^53/(2e8*86400*365.2425)
ans =
1.4271
years. In that case,
>> ticks = 1234567890123456;
>> datetime(2017,1,1,'Format','yyyy DDD\HH:mm:ss.SSSSSSSSS') + milliseconds(ticks/50000)
ans =
datetime
2017 286\18:42:37.802469120
more or less does what you want. If you need longer spans, you'd need to split the counter into two pieces. It would help to have more information.
Your format is kind of unusual, with dots in the fractional seconds. You can not currently do that with datetime, the fractional seconds must be contiguous. Is this a standard format? If all you are looking for is text, it should be simple enough to insert those dots.
  1 Comment
Matthew Tourtelot
Matthew Tourtelot on 10 May 2017
The counter is 64-bit, 20ns LSB time since 00:00:00 on 1 JAN 2000 from AltaView 1553 CDP software.
The export format is specific for a data review program we use. Would converting to double effect the precision? I'm not following the first equation you posted. This does seem like a useful approach, but I need to ensure the precision down to 20ns.
From there, just converting to string and adding the dots wouldn't be a problem, as long as this approach is in fact more efficient.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!