Get UNIX standard timestamp on MATLAB
124 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Yuanhanqing Huang
am 9 Jan. 2019
Beantwortet: Michael Nawenstein
am 9 Jun. 2020
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 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
James Tursa
am 9 Jan. 2019
Bearbeitet: James Tursa
am 9 Jan. 2019
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 Kommentar
Steven Lord
am 10 Jan. 2019
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.
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!