time in seconds to standard time
6 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(2 个)
Steven Lord
2017-5-10
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 个评论
Steven Lord
2017-5-10
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.
Peter Perkins
2017-5-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!