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?

 采纳的回答

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 个评论

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'
Having isdst return an empty array when the timezone is missing would avoid this very natural misinterpretation of the output.
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.
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.
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 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by