Tried this today in Greenville Texas (CST)
>> isdst(datetime('now'))
0
Everything I read says Daylight Savings Time is from 2nd Sunday in March to 1st Sunday in November, so we should be in Daylight Savings Time now...
Have tried this with other dates too; i.e.,
>>isdst(datetime('062316','Format','MMddyy'))
0
23Jun2016 should have been in DST.
Looks like this is buggy?

 채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 8일

1 개 추천

datetime('now') is an example of an "unzoned" time -- it has no timezone attached. isdst() only works for datetime objects that have timezones.
>> isdst(datetime('now', 'timezone', 'America/Winnipeg'))
ans =
logical
1

댓글 수: 5

Steven Lord
Steven Lord 2019년 8월 9일
Walter is correct. As stated in the documentation "isdst returns false for all elements when the TimeZone property of t is empty ('')."
>> N = datetime('now');
>> isdst(N)
ans =
logical
0
>> N.TimeZone
ans =
0×0 empty char array
>> N2 = datetime('now', 'TimeZone', 'America/New_York');
>> isdst(N2)
ans =
logical
1
>> N2.TimeZone
ans =
'America/New_York'
the cyclist
the cyclist 2019년 9월 5일
Having isdst return an empty array when the timezone is missing would avoid this very natural misinterpretation of the output.
Steven Lord
Steven Lord 2019년 9월 6일
But then you would need to follow every isdst call that you made with a check whether the output was the same size as the input to the isdst call or was empty then check (if it wasn't empty) whether the element(s) in which you were interested were true or false.
the cyclist
the cyclist 2019년 9월 10일
Yeah, empty was a silly idea. My original idea, before writing what I did, was to output NaN. Then I decided that that wasn't quite right, and reporting "the timezone is empty" made more sense. But then forgot that that would collapse the vector.
But, I think I'd prefer NaN to false.
Walter Roberson
Walter Roberson 2019년 9월 10일
Returning NaN would change the datatype of the return. You would also have to store the result in a variable and use isnan() on it first, without being able to just use
if isdst(...)
because nan is not convertable to logical for if purposes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

질문:

2019년 8월 8일

댓글:

2019년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by