Will you show me examples of changing time zones with datetime?

78 次查看(过去 30 天)
I am currently working with data that comes in GMT. I want to get averages of when an event began and ended in local time (which is pacific standard time, a difference of 7 or 8 hours depending on the time of year). I don't want to just subtract 7 or 8, because datetime keeps track of daylight savings time, which is super helpful.
I change my time with the following code:
dateHourlyTZ = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles','Format','d-MMM-y HH:mm:ss Z');
And as a result, I get dates that look like the following:
'1-Dec-1994 15:00:00 -0800'
'1-Dec-1994 16:00:00 -0800'
'1-Dec-1994 17:00:00 -0800'
'1-Dec-1994 18:00:00 -0800'
'2-Dec-1994 11:00:00 -0800'
'2-Dec-1994 15:00:00 -0800'
'5-Dec-1994 15:00:00 -0800'
'5-Dec-1994 16:00:00 -0800'
'18-Dec-1994 16:00:00 -0800'
'18-Dec-1994 17:00:00 -0800'
'23-Dec-1994 15:00:00 -0800'
'23-Dec-1994 16:00:00 -0800'
'23-Dec-1994 17:00:00 -0800'
'27-Dec-1994 15:00:00 -0800'
This is great except for the fact that it doesn't actually subtract the 8 hour time difference from the time. I considered maybe converting it to datestring and then manually subtracting the difference (whether it's 7 or 8 hours for the time of year), but when I convert it to datestring, I lose the -0800 or -0700.
The truth is I think I just don't understand all of what I can do with datetime or how the timezone change is applied. I've read all the Mathworks's info on datetime, but if you have any examples of how to manipulate timezones better or have any additional insight, that would be great. I'm not finding useful examples right now online!
Thanks a lot! -- Ellyn

采纳的回答

Walter Roberson
Walter Roberson 2016-10-24
If you have not set the TimeZone on a datetime before, then it does not assume UTC to be converted. Setting the TimeZone for such an object does not change the associated time: it leaves the time alone and fills in the TimeZone field.
For an object that has a TimeZone filled out, changing the TimeZone will changed the displayed time.
  10 个评论
K E
K E 2016-11-4
This worked example is very helpful. Where do I go to suggest a similar example gets added to the online datetime documentation?

请先登录,再进行评论。

更多回答(2 个)

Marc Jakobi
Marc Jakobi 2016-10-24
编辑:Marc Jakobi 2016-10-24
Setting the TimeZone property doesn't change the time of a datetime vector, it just tells Matlab which time zone the datetime object is in. So setting
dateHourlyLocal, 'TimeZone','America/Los_Angeles')
will only change the property (and maybe the dates at which daylight savings occurs), but not the time itself. The TimeZone property is especially useful if you have two datetime vectors from two different time zones. For example, subtracting two datetimes with the same value - one in New York, one in Berlin - will result in 06:00:00 (the time difference between the two time zones).
So if you would like to convert the time, you could do something like
dt = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles') - dateHourlyLocal;
dateHourlyTZ = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles') - dt;
Or the other way round (depending on whether you would like to add or subtract time). You may have to experiment with this a bit.
  1 个评论
Ellyn Gray
Ellyn Gray 2016-10-25
Hi Marc,
Thanks for your thoughts. Your method gives me the same output I had above. I should have mentioned that I am telling Matlab which time zone the data is set in with the following code:
dateHourlyLocal = datetime(dateHourlyReal, 'TimeZone','local','Format','d-MMM-y HH:mm:ss Z');
dateHourlyLocal.TimeZone='Etc/UTC';
dateHourlyTZ = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles','Format','d-MMM-y HH:mm:ss Z');
The challenge I've had both in applying your idea here (subtracting dt) as well as my own objective (trying to find the mean), is that datetime doesn't seem to retain this TimeZone change when doing arithmetic. Thus when I tried to do
dt = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles') - dateHourlyLocal;
(which would be the correct subtraction) ... it said that the solution was 0 for all entries.
Currently my dateHourlyTZ looks the same as it does using your code:
1-Dec-1994 15:00:00 -0800
1-Dec-1994 16:00:00 -0800
1-Dec-1994 17:00:00 -0800
but I would prefer it look like this:
1-Dec-1994 07:00:00
1-Dec-1994 08:00:00
1-Dec-1994 09:00:00
so that when I calculated a mean for each month, it would calculate it in the correct time and applying the correct daylight savings change.
If you have any ideas, let me know!

请先登录,再进行评论。


Al in St. Louis
Al in St. Louis 2018-5-23
So, I have to tell MATLAB that the timezone is London if I have UTC times. That's not very intuitive.
  3 个评论
Al in St. Louis
Al in St. Louis 2019-12-4
This doesn't help me as I have times that are UTC, but I have not figured out how to tell MATLAB that.
Steven Lord
Steven Lord 2019-12-4
Natick, MA (the location of MathWorks HQ) is in the America/New_York time zone, as I can see by comparing a datetime created by specifying the 'local' time zone with one in America/New_York.
tLocal = datetime('now', 'TimeZone', 'local');
tNY = datetime('now', 'TimeZone', 'America/New_York');
If I view the information from the list of time zones for the UTC offset for America/New_York it's -5. We're not in Daylight Savings Time right now, so I can ignore the DSTOffset.
tz = timezones;
tz(tz.Name == "America/New_York", :)
So if I convert tNY to UTC, or create a new datetime in the UTC time zone, the time should be five hours different. I can do this using the 'Etc/UTC' time zone.
tUTC = datetime('now', 'TimeZone', 'Etc/UTC');
tNYToUTC = tNY; tNYToUTC.TimeZone = 'Etc/UTC';
If you display these datetime values tLocal and tNY should have (very close to, modulo how long it takes you to type the command) the same time, and tUTC and tNYToUTC should have a time that's (very close to) five hours different.
tLocal, tNY, tUTC, tNYToUTC

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by