How to ensure my times will follow daylight savings?
9 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
回答(2 个)
Sulaymon Eshkabilov
2019-5-13
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.
Peter Perkins
2019-6-4
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
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!