Switching TimeZone property in datetime function doesn't change output.

7 次查看(过去 30 天)
Hello,
I'm using this command to get a datetime output for a timetable.
startdatum=datetime(6.375400481428600e+10,...
'ConvertFrom','epochtime','Epoch','01.01.0001 00:00:00.000',...
'TimeZone','Europe/London',...
'Format','dd.MMM.yyyy HH:mm:ss.SSS', ...
'InputFormat','dd.MMM.yyyy HH:mm:ss.SSS');
Switching the property TimeZone to 'Europe/London' or even 'America/Denver' within the datetime function doesn't change the output?
Only after executing the command and entering startdatum.TimeZone = 'Europe/London'(or any other timezone) in the command window actually change the startdatum variable.
Could someone explain to me, if it's an error from Matlab or is it the way this command works using EpochTime?
I tried: startdatum=datetime(6.375400481428600e+10,...
'ConvertFrom','posixtime',...
'TimeZone','Europe/Zurich',...
'Format','dd.MMM.yyyy HH:mm:ss.SSS');
and changing the TimeZone resulted in different outputs.

采纳的回答

Peter Perkins
Peter Perkins 2022-2-16
Marius, here's what's happening:
The first few inputs of your code tell datetime that the instant of time you are talking about is 6.375400481428600e+10 seconds after 01-01-0001 00:00:00. That ends up looking like 14.Apr.2021 13:46:54.286. But there's no timezone attached to that origin, so at that point, it's unzoned. Then datetime sees 'TimeZone','Europe/London', and puts the instant into that TZ, but preserves the clockface time. In other words, if the epoch is specified as unzoned, then regardless of what you pass in as 'TimeZone', the result will always look like 14.Apr.2021 13:46:54.286.
Let's say 6.375400481428600e+10 is supposed to represent elapsed seconds UTC. Here's what you would do:
>> fmt = 'dd.MMM.yyyy HH:mm:ss.SSS z';
>> dt1UTC = datetime(0001,1,1,'TimeZone','UTC');
>> dt = datetime(6.375400481428600e+10, ...
'ConvertFrom','epochtime','Epoch',dt1UTC, ...
'Format',fmt, ...
'TimeZone','Europe/London')
dt =
datetime
14.Apr.2021 14:46:54.286 UTC+1
>> dt = datetime(6.375400481428600e+10, ...
'ConvertFrom','epochtime','Epoch',dt1UTC, ...
'Format',fmt, ...
'TimeZone','Europe/Zurich')
dt =
datetime
14.Apr.2021 15:46:54.286 UTC+2

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by