Why does datetime return repeated values?
1 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2019-9-5
回答: MathWorks Support Team
2020-1-27
When I execute the following code snippet in MATLAB
start_time = datetime('05-Nov-2017 00:00:00','TimeZone','America/Los_Angeles');
end_time = datetime('05-Nov-2017 03:00:00','TimeZone','America/Los_Angeles');
DateString = start_time:1/2/24:end_time;
This ought to result in 7 times: 00:00, 00:30, 1:00, 1:30, 2:00, 2:30, and 3:00 on the same day.
Instead, it results in 9 times: 00:00, 00:30, 1:00, 1:30, 1:00, 1:30, 2:00, 2:30, and 3:00 on the same day.
If I do the same thing for the previous day, Nov 4, 2017, everything is fine...
start_time = datetime('04-Nov-2017 00:00:00','TimeZone','America/Los_Angeles');
end_time = datetime('04-Nov-2017 03:00:00','TimeZone','America/Los_Angeles');
DateString = start_time:1/24:end_time;
Gives 1 x 4 datetime array: 04-Nov-2017 00:00:00 04-Nov-2017 01:00:00 04-Nov-2017 02:00:00 04-Nov-2017 03:00:00
How can I explain this behavior?
采纳的回答
MathWorks Support Team
2019-9-5
This is due to Daylight Saving Time (DST).
In 2017, for the United States, Daylight Saving Time (DST) ended on Sunday, November 5. Clocks were set back 1 hour to standard time from 02:00 (2 am) to 01:00 (1 am), local time on this day.
This leads to the repeated values, which is the expected behavior.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!