Converting Unix Timestamp to Date Time
137 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I'm trying to convert Unix time stamps to date time. So far I have tried:
date_time=(timestamp_array./86400000) + datetime(1970,1,1);
This seems to work find, except that my first measurment was 2123 seconds off. I looked at a few other times and they were 2122 seconds off, 2125 seconds off, etc. I'm not sure what could be causing this discrepancy.
Here is my original time data:
And here is what I am getting after I use my code:
Any insights would be much appreciated!!
(I had to divide my timestamps by 86400000 instead of 86400 becuase all my timestamsps have 3 extra zeros for some reason. Also, all measurements are on the same day if that makes any difference)
2 commentaires
James Tursa
le 12 Juin 2024
"... all my timestamsps have 3 extra zeros for some reason ..."
Because the timestamps are in milliseconds, not seconds. But this is not unusual for time counters to be using finer units than seconds.
Réponse acceptée
Steven Lord
le 12 Juin 2024
Use the 'ConvertFrom' option in your datetime call. This tells datetime how to interpret the value you pass into it. I think you want either 'posixtime' or perhaps the 'epochtime' option with both 'epoch' and 'TicksPerSecond'.
datetime(1.7e12, 'ConvertFrom', 'posixtime')
datetime(1.7e12/1000, 'ConvertFrom', 'posixtime') % Note the division by 1000
datetime(1.7e12, 'ConvertFrom', 'epochtime', 'epoch', datetime(1970, 1, 1), 'TicksPerSecond', 1000)
I used 1.7e12 which is only an approximation to your value, which is why it represents a date in 2023 rather than one in 2024.
2 commentaires
James Tursa
le 12 Juin 2024
Modifié(e) : James Tursa
le 13 Juin 2024
See my comment to @Cris LaPierre. Bottom line is IMO one should always specify a TimeZone of 'UTC' when doing posixtime conversions to avoid potential downstream conversion issues.
Plus de réponses (1)
Cris LaPierre
le 12 Juin 2024
Set the 'datatype' option to 'posixtime'.
As an example, the Unix time stamp 1718195288 is Wed Jun 12 2024 12:28:08 GMT+0000
t = 1718195288;
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','')
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','America/New_York')
2 commentaires
James Tursa
le 12 Juin 2024
Modifié(e) : James Tursa
le 12 Juin 2024
You should never use an unzoned TimeZone of '' when converting from posixtime. Since posixtimes are defined in terms of a duration from a specific UTC time, that is what should be used to avoid potential downstream problems. E.g.,
t = 1718195288;
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','UTC')
Then subsequent time zone conversions will work properly. E.g.,
datetime(dt,'TimeZone','America/New_York')
Or one could specify a specific different TimeZone in the initial conversion from posixtime as shown in your example. But if you use an unzoned '' TimeZone initially, you will get wrong answers for subsequent attempted naive conversions. E.g.,
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','')
% The following *attaches* a TZ to an unzoned datetime ... it doesn't convert!
datetime(dt,'TimeZone','America/New_York') % WRONG!
% This is what you would have to do instead:
datetime(datetime(dt,'TimeZone','UTC'),'TimeZone','America/New_York')
Now you get the correct result. IMO it is much better to simply attach the proper UTC TimeZone to begin with in the initial posixtime conversion so that downstream code automatically accounts for proper TimeZone stuff.
Frankly, I would have hoped that datetime() would have automatically attached the 'UTC' TimeZone in the following conversion, but unfortunately it doesn't:
dt = datetime(t,'ConvertFrom','posixtime')
dt.TimeZone
So you are forced to manually enter a TimeZone in the initial posixtime conversion if you want to avoid the potential problems I show above. And as a personal preference issue, I always like to see the TimeZone in the display format. E.g.,
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','UTC')
dt.Format = [dt.Format ' z']
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!