How to ensure my times will follow daylight savings?

9 visualizzazioni (ultimi 30 giorni)
Hi everyone,
I am working on a piece of code which will allow me to reduce a dataset, and part of this requires converting separate date and time variables into one datenum variable.
I have the time in UTC time and also local time. I want to keep local time (Pacific/Auckland) only, but I want to ensure that this will be correct throughout the year/daylight savings. How do I do this?
I have set the timezone on the local data but from what I can tell, this just labels the data? It doesn't actually change the timezone? (I tried it previously with the UTC time data I have and the data remained the same but the "timezone" changed.)
In my data I have UTC time, UTC date, localtime and localdate variables but I am working with the local data in this example as this is what I am interested in ultimately. I will be working with large sets of data that I want to know the date/time of-this data will always be recorded in this area but will span years. I mention this as I'm not sure if it's preferrable to work with UTC time for this purpose.
So, everything is working right now but I'm not sure if my code is robust to give accurate datenumbers year round?
Thanks in advance!
Louise
data=readtable(filename);
try
fid = fopen(fullfile(dd,filename)); %open file
%% Put Date and Time into One Column
dates=datetime(data.LOCALDATE, 'TimeZone', 'Pacific/Auckland',...
'Format', 'y/M/d'); %convert date to datetime array
times=datenum(data.LOCALTIME); %datenum-serial date number
t=table(dates,times); %create 2x table of dates and times
dates=datetime(t.dates,'Format', 'y/M/d hh:mm:ss'); %format date cells
times=datetime(t.times,'ConvertFrom','datenum','Format',... %format time cells
'y/M/d hh:mm:ss');
fullt=dates+timeofday(times); %date and time into one column
data.DateTime=fullt; %append column onto data table
%% Convert DateTime to DateNum
DateNumber=datenum(data.DateTime); %convert datetime to datenumber
data.DateNumber=DateNumber;

Risposte (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 13 Mag 2019
Hi,
UTC is a good approach and your code seems to be considering all possible case scenarios w.r.t time zones with your employed 'TimeZone' option while defining date and time.
good luck.
  1 Commento
Louise Wilson
Louise Wilson il 13 Mag 2019
Hi Sulaymon,
Thank you. I tried the same code inputting UTCtimes where my local times.
I have four coloumns of data: UTC DATE, UTC TIME, LOCAL TIME, LOCAL DATE.
Ultimately I want to reduce these for to one datenum column in local Pacific/Auckland time, but have this set so it can adjust depending on time of year/daylight savings.
I have tried the above code with UTC time...
%% Put Date and Time into One Column
localdates=datetime(data.UTCDATE, 'TimeZone',...
'Pacific/Auckland','Format', 'y/M/d'); %convert date to datetime array, setting time zone
localtimes=datenum(data.UTCTIME); %datenum-serial date number
t=table(localdates,localtimes); %create 2x table of dates and times
dates=datetime(t.localdates,'Format', 'y/M/d hh:mm:ss'); %format date cells
times=datetime(t.localtimes,'ConvertFrom','datenum','Format',... %format time cells
'y/M/d hh:mm:ss');
fullt=dates+timeofday(times); %date and time into one column
data.DateTime=fullt; %append column onto data table
...but the values this gives me are still UTC time. Inputting the time zone parameters to datetime does not change the UTC time zone from UTC to Pacific/Auckland. Is this what you would expect? Is there a way to convert the data from one time zone to another? As perhaps this is what I need to ensure my code is robust year-round?
Thank you for your help

Accedi per commentare.


Peter Perkins
Peter Perkins il 4 Giu 2019
The answer is don't use datenums. They do not support time zomes at all. Use datetimes.
If you set the TimeZone property on your datetime arrays, everything will just work: accounting for time differences between UTC and Auckland, differences between Auckland and London, shifts for DST. You don't need to do anything else.
Just to cklarify something that I think may have been confusing you: If you create a datetime without specifying a time zone, it is "unzoned". If you then set its .TimeZone property, the clockface time remians the same, but it is "in" a time zone. Then if you change the .TimeZone, the clockfcase time changes appropriately.
>> dt = datetime('now','Format',"dd-MMM-uuuu HH:mm:ss z")
dt =
datetime
04-Jun-2019 09:58:26 *
>> dt.TimeZone = 'America/New_York'
dt =
datetime
04-Jun-2019 09:58:26 EDT
>> dt.TimeZone = 'Pacific/Auckland'
dt =
datetime
05-Jun-2019 01:58:26 UTC+12
>> dt.TimeZone = 'UTC'
dt =
datetime
04-Jun-2019 13:58:26 UTC

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by