Convert date to UNIX time

조회 수: 100 (최근 30일)
James
James 2011년 4월 24일
편집: James Tursa 2023년 7월 22일
Hi,
What combination of date functions do I need to use to convert a date such as '24-Apr-2011' into UNIX time (and vice versa)?
I've been playing around with datenum, datestr and datetick but I can't figure it out
Thanks Dave
  댓글 수: 7
Stephen23
Stephen23 2023년 7월 22일
편집: Stephen23 2023년 7월 22일
" I was nevertheless surprised that matlab didn't have built-in routines to perform these two tasks (inputting and outputting a date as seconds ellapsed from a specific start date and time)."
Of course it does, you can find them listed in the MATLAB documentation:
TN = datetime('now');
S0 = seconds(TN - datetime(1970,1,1)) % Your approach
S0 = 1.6901e+09
S1 = posixtime(TN) % inbuilt MATLAB function
S1 = 1.6901e+09
S2 = convertTo(TN,'posix') % inbuilt MATLAB function
S2 = 1.6901e+09
isequal(S0,S1,S2)
ans = logical
1
And if you really want to control the epoch and ticks per second (but lossy due to the numeric operations):
S3 = convertTo(TN,'epochtime','Epoch','1970-1-1', 'TicksPerSecond',1); % inbuilt MATLAB function
S3 = double(S3)
S3 = 1.6901e+09
And the reverse conversion is easy too:
T0 = datetime(1970,1,1) + seconds(S0) % your approach
T0 = datetime
22-Jul-2023 19:01:44
T1 = datetime(S0,'ConvertFrom','posix') % inbuilt MATLAB function
T1 = datetime
22-Jul-2023 19:01:44
T2 = datetime(S0,'ConvertFrom', 'epochtime','Epoch','1970-1-1','TicksPerSecond',1) % inbuilt MATLAB function
T2 = datetime
22-Jul-2023 19:01:44
The conversion to POSIX-double is lossy, so we do not expect exact equivalence (less than ns is not bad though):
milliseconds(T0-TN)
ans = -5.0783e-05
milliseconds(T1-TN)
ans = -5.0783e-05
milliseconds(T2-TN)
ans = -1.6141e-04
James Tursa
James Tursa 2023년 7월 22일
편집: James Tursa 2023년 7월 22일
For completeness of this thread, I suppose I should add that this entire discussion with Unix Epoch of 1970-1-1 00:00 UTC assumes the redefinition of Unix Epoch using backwards extension of Modern UTC (TAI based), which didn't start until 1972. The original Unix Epoch based on Old UTC (UT based) is actually about 2 seconds different. But unless you are doing some type of historical work, you should use the redefinition with backwards extension of Modern UTC. The MATLAB datetime( ) function assumes this backwards extension. I am unaware of any MATLAB functions that use the Old UTC definition, so if you are doing historical work you will probably have to write your own conversion function.

댓글을 달려면 로그인하십시오.

채택된 답변

Peter Perkins
Peter Perkins 2015년 9월 28일
In R2014b or later, use datetime:
>> d = datetime('24-Apr-2011')
d =
24-Apr-2011
>> posixtime(d)
ans =
1303603200
>> datetime(1303603200,'ConvertFrom','posixtime')
ans =
24-Apr-2011 00:00:00
Similar syntaxes for Excel serial date numbers, Julian date numbers, and not surprisingly MATLAB date numbers.
Hope this helps.
  댓글 수: 1
Brendan Hamm
Brendan Hamm 2018년 2월 27일
Probably worth noting that time zone will have an effect as well as the offset from UTC plays a role in the appropriate conversion.
>> d1 = datetime('24-Apr-2011');
>> p1 = posixtime(d)
p1 =
1.303603200000000e+09
>> d2 = datetime('24-Apr-2011','TimeZone','America/New_York');
>> p2 = posixtime(d2)
p2 =
1.303617600000000e+09

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 4월 24일
int32(floor(86400 * (datenum('24-Apr-2011') - datenum('01-Jan-1970'))))
Note that both systems have the problem of not dealing with leap seconds.
Also, the int32() rather than uint32() is not an error: unix time was defined as a signed number.
  댓글 수: 5
Walter Roberson
Walter Roberson 2015년 9월 28일
Ah, my answer is for time_t in particular. I have never encountered a Unix system that used a different representation.
James Tursa
James Tursa 2015년 9월 28일
Yes, for time_t I agree that it is typically implemented as a signed 32-bit integer.

댓글을 달려면 로그인하십시오.

카테고리

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