Convert date to UNIX time

102 vues (au cours des 30 derniers jours)
James
James le 24 Avr 2011
Modifié(e) : James Tursa le 22 Juil 2023
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 commentaires
Stephen23
Stephen23 le 22 Juil 2023
Modifié(e) : Stephen23 le 22 Juil 2023
" 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 le 22 Juil 2023
Modifié(e) : James Tursa le 22 Juil 2023
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.

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 28 Sep 2015
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 commentaire
Brendan Hamm
Brendan Hamm le 27 Fév 2018
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

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 24 Avr 2011
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 commentaires
Walter Roberson
Walter Roberson le 28 Sep 2015
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 le 28 Sep 2015
Yes, for time_t I agree that it is typically implemented as a signed 32-bit integer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by