UTC Time to String Conversion

datetime, matlab, time, utc

Solution

Unix time today is about 13e11, and is measured in ms since 1970.

If your time is ~3e11, then it's probably since year 2000.

>> time_unix = 1339116554872; % example time
>> time_reference = datenum('1970', 'yyyy'); 
>> time_matlab = time_reference + time_unix / 8.64e7;
>> time_matlab_string = datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF')

    time_matlab_string =

    20120608 00:49:14.872

Notes:

1) change 1970 into 2000 if your time is since 2000;

2) See the definition of matlab's time.

3) 8.64e7 is number of milliseconds in a day.

4) Matlab does not apply any time-zone shifts, so the result is the same UTC time.

5) Example for backward transformation:

>> matlab_time = now;
>> unix_time = round(8.64e7 * (matlab_time - datenum('1970', 'yyyy')))

unix_time =

             1339118367664

Problem

I am looking for helping doing time conversions from UTC time to string using MATLAB. I am trying to extract time from a data file collected at the end of October 2010. The data file says it is reporting in UTC time and the field is an integer string value in milliseconds that is around 3.02e11. I would like to convert this to a string but am have some trouble. I figured out that the units are most definitely in milliseconds so I convert this to fractions of days to be compatible with datenum format. If the data was collected at the end of October (say, October 31, 2010) then I can guess what kind of number I might get. I thought that January 1, 2001 would be a good epoch and calculated what sort of number (in days) I might get: ``` suspectedDate = datenum('October 31, 2010') suspectedEpoch = datenum('January 1, 2001') suspectedTimeInDays = suspectedDate - suspectedEpoch ``` Which comes out as 3590. However, my actual time, in days, comes out with the following code ``` actualTime = 3.02e11 actualTimeInDays = 3.02e11/1000/24/3600 ``` as 3495.4. This is troubling as the difference is only 94.6 -- not a full year. This would mean either the documentation for the file is wrong or the epoch is close to April 1-5, 2001: ``` calculatedEpoch = suspectedDate - actualTimeInDays calculatedEpochStr = datestr(calculatedEpoch) ``` Alternately, if the epoch is January 1, 2001 then the actual date in the file is from the end of July. ``` ifEpochIsJanuaryDate = suspectedEpoch + actualTimeInDays ifEpochIsJanuaryDateStr = datestr(ifEpochIsJanuaryDate) ``` Is this a known UTC format and can anyone give suggestions on how to get an October date from 3.02e11 magnitude number?

Original source