Get UNIX standard timestamp on MATLAB
164 次查看(过去 30 天)
显示 更早的评论
Hello to everybody!
I got a question about how to get UNIX standard timestamp on MATLAB, which is the number of seconds from Jan 1 1970.
By referring to MATLAB website, I think `now` is the command that generate the number of days from Jan 1 0000.
But what I want is to get the timestamp that is the same to the one generated by time.time() in Python.
2 个评论
采纳的回答
更多回答(2 个)
James Tursa
2019-1-9
编辑:James Tursa
2019-1-9
You need to be careful about how you use the posixtime(t) function. From the documentation:
"...If the time zone of t is not specified, then posixtime treats the times in t as UTC times..."
The definition of Unix Time is specifically counted from 01-Jan-1970 00:00:00 UTC, but MATLAB functions such as now( ) use your local computer time which likely has a different time zone than UTC. So if you are working with now( ) or any time stamp data that is based on some local computer time but doesn't have the time zone explicitly listed, you will need to account for the time zone to convert to Unix Time correctly. E.g., on my system here is one way to get the time zone of the local computer clock:
>> import java.util.TimeZone
>> TimeZone.getDefault().getID()
ans =
America/Los_Angeles
This is what would have to be used in conjunction with the MATLAB now( ) function to get a correct conversion to Unix Time. E.g.,
>> n = now
n =
7.374345779739352e+05
>> ds = datestr(n)
ds =
'09-Jan-2019 13:52:16' % round to seconds for simplicity of the example
>> dt = datetime(ds,'TimeZone',char(TimeZone.getDefault().getID()))
dt =
datetime
09-Jan-2019 13:52:16
>> posixtime(dt)
ans =
1.547070736000000e+09
The wrong way to do it, without telling posixtime( ) about the time zone information, is as follows:
> posixtime(datetime(ds)) % No time zone information is present in the input
ans =
1.547041936000000e+09 % Gets the WRONG ANSWER because incorrectly assumes input time was UTC
And, just for completeness, a manual calculation knowing that my local 'America/Los_Angeles' timezone is 8 hours different from UTC:
>> (datenum(ds) - datenum('01-Jan-1970 00:00:00') + 8/24) * 86400
ans =
1.547070735999995e+09
This method has a slight numerical difference from the first method because datenums, which use doubles, don't have the same precision as the datetimes do.
I haven't checked, but I would assume that the MATLAB datetime( ) function understands all of the timezone strings that can be produced with the java TimeZone.getDefault().getID() method.
See also the discussion here:
1 个评论
Steven Lord
2019-1-10
You can specify that you want to create the datetime in the system time zone.
dt = datetime('now', 'TimeZone', 'local')
See the description of the TimeZone property on the datetime documentation page or the timezones function for information about other values that you can specify.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!