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 ExchangeDates and Time についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by