datenum - Parse ISO 8601 timestamp without applying UTC offset

조회 수: 28 (최근 30일)
Randall Pittman
Randall Pittman 2015년 6월 17일
댓글: Randall Pittman 2015년 6월 18일
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