Get UNIX standard timestamp on MATLAB

조회 수: 166 (최근 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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by