how to convert 18 digits timestamp to readable date and time in MATLAB

I have data with a time step consists of 18 digits such as 634019142119225390. This date should be in February 2010 by the way.
How can i convert this into readable date and time in MATLAB ?
Unfortunately i don't know what is the format of this timestamp. Is it UNIX or Julian or what.
Many Thanks in advance

6 Comments

Number of seconds since the big bang?
Maybe with more samples one could deduce what the measurement unit is and then the base time. With one sample the cyclists guess is as good as any. I might guess that the measurements are in mico or nano seconds though.
It's the time in 100-nanoseconds since Jan 1, 1 UTC.
Thank you all for your comments. More samples are provided.
634019142119225390
634019142129597610
634019142139821660
I'm not sure why you're providing more samples. As far as I can tell, I've answered your question.
I added more samples as required by @ Brendan Hamm

Sign in to comment.

 Accepted Answer

This page may be useful.
Most likely, your timestamp is a .Net System.DateTime
>>ts = int64(634019142119225390);
>>dt = System.DateTime(ts); %Note that you need to be on Windows for this to work
>>dt.ToString
ans =
16/02/2010 00:00:00

5 Comments

Thank you for your kind reply.
How can i do the same for multiple timestamps ?
I made a new variable in the workspace contains my timestamps then
>>ts = int64(variable);
>>dt = System.DateTime(ts);
But i receive an error ' Error using System.DateTime. No constructor 'System.DateTime' with matching signature found.'
The simplest way would be to use arrayfun (or a loop). You can't pass arrays to System.DateTime
tss = [int64(634019142119225390) int64(634019142129597610) int64(634019142139821660)]; %see note below
dts = arrayfun(@System.DateTime, tss, 'UniformOutput', false);
dtastring = cellfun(@(dt) char(dt.ToString), dts, 'UniformOutput', false)
Note: because you're dealing with 64-bit integers that cannot be represented exactly as double, you have to be extremely careful not to use double even temporarily. I strongly suspect that your line ts = int64(variable) does not work. In particular
v = [634019142119225390 634019142129597610 634019142139821660]; v is double
ts = int64(v)
does not work, because of the storage as double. And
ts = int64([634019142119225390 634019142129597610 634019142139821660])
does not work either because the array is constructed as double.
Thank you very much for your reply and help.
why these two timestamps give me the same date and time ?
634018095130693130 and 634018095139168740
As per the documentation of System.DateTime, its resolution is 100 nanoseconds. Therefore the difference between your two timestamps is only 1 microsecond.
You need to add 10,000,000 (1e7) to just go up one second.
Thank you very much Guillaume for your help.

Sign in to comment.

More Answers (2)

Another option, if you have MATLAB R2014b or later, is this:
>> x = [uint64(634019142119225390) uint64(634019142129597610) uint64(634019142139821660)]
x =
634019142119225390 634019142129597610 634019142139821660
>> datetime(double(x)/1e7,'ConvertFrom','epochtime','Epoch','1-Jan-0001','Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS')
ans =
16-Feb-2010 10:50:11.922531250 16-Feb-2010 10:50:12.959757812 16-Feb-2010 10:50:13.982171875
But that's not exactly right -- those uint64's are larger than flintmax, so casting them to double introduces round-off. You may not care about 100ns resolution. If you only cared about 1ms resolution, you could do this:
>> datetime(double(x/1e4)/1e3,'ConvertFrom','epochtime','Epoch','1-Jan-0001','Format','dd-MMM-yyyy HH:mm:ss.SSS')
ans =
16-Feb-2010 10:50:11.923 16-Feb-2010 10:50:12.960 16-Feb-2010 10:50:13.982
But to get exactly what you started with, do this:
>> secs = (x-.5e7)/1e7
secs =
63401914211 63401914212 63401914213
>> milliSecs = double(x - uint64(secs)*1e7)/1e4
milliSecs =
922.539 959.761 982.166
>> datetime(secs,'ConvertFrom','epochtime','Epoch','1-Jan-0001','Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS') + milliseconds(milliSecs)
ans =
16-Feb-2010 10:50:11.922539000 16-Feb-2010 10:50:12.959761000 16-Feb-2010 10:50:13.982166000
634019142119225390 appears to be the number of nanoseconds that has elapsed since 01/01/0001 00:00. Here is a quick Powershell to convert that to human readable:
$timestamp = 636244407153688066 / 10000000
$epochDate = [datetime]"01/01/0001 00:00"
$epochDate.AddSeconds($timestamp)

Asked:

on 23 Apr 2015

Answered:

on 8 Mar 2017

Community Treasure Hunt

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

Start Hunting!