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?

 Réponse acceptée

Walter Roberson
Walter Roberson le 8 Août 2019

1 vote

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 commentaires

Steven Lord
Steven Lord le 9 Août 2019
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 le 5 Sep 2019
Having isdst return an empty array when the timezone is missing would avoid this very natural misinterpretation of the output.
Steven Lord
Steven Lord le 6 Sep 2019
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 le 10 Sep 2019
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 le 10 Sep 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by