Get UNIX standard timestamp on MATLAB

54 ビュー (過去 30 日間)
Yuanhanqing Huang
Yuanhanqing Huang 2019 年 1 月 9 日
回答済み: Michael Nawenstein 2020 年 6 月 9 日
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 件のコメント
Yuanhanqing Huang
Yuanhanqing Huang 2019 年 1 月 9 日
編集済み: Yuanhanqing Huang 2019 年 1 月 9 日
num2str(posixtime(datetime('now')) * 1e6)
Get current UNIX timestamp. Thanks for Steven's help!
James Tursa
James Tursa 2019 年 1 月 9 日
This is NOT correct. See my Answer below.

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

採用された回答

Steven Lord
Steven Lord 2019 年 1 月 9 日

その他の回答 (2 件)

James Tursa
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
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.

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


Michael Nawenstein
Michael Nawenstein 2020 年 6 月 9 日
floor(posixtime(datetime('now','TimeZone','local'))

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by