Convert date to UNIX time

102 次查看(过去 30 天)
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