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

7 views (last 30 days)
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.

Accepted Answer

Peter Perkins
Peter Perkins on 16 Feb 2022
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

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!