datenum - Parse ISO 8601 timestamp without applying UTC offset

38 views (last 30 days)
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 Comments
per isakson
per isakson on 17 Jun 2015
Edited: per isakson on 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 on 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

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 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 Comment
Randall Pittman
Randall Pittman on 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.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!