Filter löschen
Filter löschen

datenum - Parse ISO 8601 timestamp without applying UTC offset

26 Ansichten (letzte 30 Tage)
Randall Pittman
Randall Pittman am 17 Jun. 2015
Kommentiert: Randall Pittman am 18 Jun. 2015
I'm using MATLAB R2014b-win64
When I call datenum with an ISO 8601 datenum, I get:
>> result = datenum('2015-05-01T00:00:01Z','yyyy-mm-ddTHH:MM:SSZ')
result =
736084.666678241
But this is not correct--MATLAB assumed I wanted the time changed from UTC to my local time zone:
>> datestr(result)
ans =
30-Apr-2015 16:00:01
If I leave off the 'Z' from the formatIn field, I get the right datenum:
>> result = datenum('2015-05-01T00:00:01Z','yyyy-mm-ddTHH:MM:SS')
result =
736085.000011574
>> datestr(result)
ans =
01-May-2015 00:00:01
I don't see anything about this in the documentation. Am I missing something?
Perhaps this is a bug?
  2 Kommentare
per isakson
per isakson am 17 Jun. 2015
Bearbeitet: per isakson am 18 Jun. 2015
Neither "UTC" nor "Z" is described on the page datenum, Convert date and time to serial date number - as far as I can see. If so, we may not make any assumptions on the behavior.
Stephen23
Stephen23 am 18 Jun. 2015
You could avoid the whole issue by using my FEX submission datenum8601. It automatically detects different different format ISO 8601 timestamps and converts them to serial date numbers, with no time zone changes:
>> V = datenum8601('2015-05-01T00:00:01Z')
V =
7.3609e+05
>> datestr(V)
ans =
01-May-2015 00:00:01

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Peter Perkins
Peter Perkins am 18 Jun. 2015
Randall, this is indeed a bug, albeit for inputs that were never documented to work. Prior to R2013b, this generated an error. Although 'yyyymmddTHHMMSS' is a format documented to work in datenum/datevec/datestr, you've added a Z at the end, intended as "literal". That's the problem.
Two suggestions:
1) You're using R2014b. Consider using datetime instead of datenum:
>> datetime('2015-05-01T00:00:01Z','Format','yyyy-MM-dd''T''HH:mm:ss''Z''')
ans =
2015-05-01T00:00:01Z
Notice how I've "escaped" the two literals in the format. If you want to use time zones, datetime supports those, while datenum does not.
>> datetime('2015-05-01T00:00:01Z','TimeZone','local','Format','yyyy-MM-dd''T''HH:mm:ssz')
ans =
2015-04-30T20:00:01EDT
>> datetime('2015-05-01T00:00:01Z','TimeZone','UTC','Format','yyyy-MM-dd''T''HH:mm:ssXXX')
ans =
2015-05-01T00:00:01Z
2) Use strrep to strip the Z off your strings, and leave it out of the format.
Hope this helps.
  1 Kommentar
Randall Pittman
Randall Pittman am 18 Jun. 2015
Thanks, Peter. I guess I expected it to work with the 'Z' since it's a valid ISO 8601 date/time string, but I understand that it's not officially supported to be used that way.
I need to learn more about datetime. I'm so used to datenums that I haven't looked at it as of yet, but I will. Thanks.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by