フィルターのクリア

Converting Unix Timestamp to Date Time

137 ビュー (過去 30 日間)
Ellie
Ellie 2024 年 6 月 12 日
編集済み: James Tursa 2024 年 6 月 13 日
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 件のコメント
James Tursa
James Tursa 2024 年 6 月 12 日
"... 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.
Ellie
Ellie 2024 年 6 月 13 日
I see! Thank you!

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2024 年 6 月 12 日
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')
ans = datetime
08-Nov-55840 22:13:20
datetime(1.7e12/1000, 'ConvertFrom', 'posixtime') % Note the division by 1000
ans = datetime
14-Nov-2023 22:13:20
datetime(1.7e12, 'ConvertFrom', 'epochtime', 'epoch', datetime(1970, 1, 1), 'TicksPerSecond', 1000)
ans = datetime
14-Nov-2023 22:13:20
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 件のコメント
James Tursa
James Tursa 2024 年 6 月 12 日
編集済み: James Tursa 2024 年 6 月 13 日
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.
Ellie
Ellie 2024 年 6 月 13 日
Thank you so much for your answer! This worked really well

サインインしてコメントする。

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2024 年 6 月 12 日
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
12-Jun-2024 12:28:08
You can adjust the time to a specific timezone if you want.
dt = datetime(t,'ConvertFrom','posixtime','TimeZone','America/New_York')
dt = datetime
12-Jun-2024 08:28:08
  2 件のコメント
James Tursa
James Tursa 2024 年 6 月 12 日
編集済み: James Tursa 2024 年 6 月 12 日
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')
dt = datetime
12-Jun-2024 12:28:08
Then subsequent time zone conversions will work properly. E.g.,
datetime(dt,'TimeZone','America/New_York')
ans = datetime
12-Jun-2024 08:28:08
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','')
dt = datetime
12-Jun-2024 12:28:08
% The following *attaches* a TZ to an unzoned datetime ... it doesn't convert!
datetime(dt,'TimeZone','America/New_York') % WRONG!
ans = datetime
12-Jun-2024 12:28:08
% This is what you would have to do instead:
datetime(datetime(dt,'TimeZone','UTC'),'TimeZone','America/New_York')
ans = datetime
12-Jun-2024 08:28:08
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 = datetime
12-Jun-2024 12:28:08
dt.TimeZone
ans = 0x0 empty char array
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 = datetime
12-Jun-2024 12:28:08
dt.Format = [dt.Format ' z']
dt = datetime
12-Jun-2024 12:28:08 UTC
Ellie
Ellie 2024 年 6 月 13 日
Thanks for the comment about the time zone! Also, this is a much easier way than I was trying before

サインインしてコメントする。

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by