datenum - Parse ISO 8601 timestamp without applying UTC offset

32 次查看(过去 30 天)
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 个评论
per isakson
per isakson 2015-6-17
编辑:per isakson 2015-6-18
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 2015-6-18
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

请先登录,再进行评论。

回答(1 个)

Peter Perkins
Peter Perkins 2015-6-18
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 个评论
Randall Pittman
Randall Pittman 2015-6-18
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.

请先登录,再进行评论。

类别

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