Determine UTC Offset for a given Time Zone?

5 次查看(过去 30 天)
I am using an API to bring data into Matlab, and the timestamps are in UNIX time format. It also returns the local time zone for the data. UNIX time is based on UTC, so it convert it into a usable timestamp, I need to consider the UTC offset for the given time zone. The problem is, I can't seem to find a way to input a time zone to Matlab and have it output a UTC offset. I know that this information is available, because you can use tzoffset. But that requires a datetime input. Which puts me into a kind of catch-22. In order to convert to a datetime, I need the UTC offset. But to get the UTC offset, I need a datetime.
Is there any function that will tell me the UTC offset if I give it a timezone?
  1 个评论
James Tursa
James Tursa 2017-3-28
How do you have the time zone information? As a string of some sort that you need interpreted?

请先登录,再进行评论。

采纳的回答

Peter Perkins
Peter Perkins 2017-3-29
All you need to do is:
>> datetime(1490742843,'ConvertFrom','posixtime','TimeZone','Europe/Paris')
ans =
datetime
29-Mar-2017 01:14:03
You don't say what form your time zone info is in, but datetime is pretty forgiving. See the doc for datetime.TimeZone.

更多回答(1 个)

Jerome Blaha
Jerome Blaha 2023-5-25
Two methods that I love:
One is a call to Java directly (credit to someone else here who found the solution)
You can also set the java to different time zone inputs (Google is your friend)
java_time=java.util.Date('Thu Mar 05 09:57:01 PDT 2023'); % Alternate for PDT zone date time input
java_time=java.util.Date(); % Uses the current local date and time,
utc_offset=-java_time.getTimezoneOffset()/60; %UTC offset from local time zone, only verified in Matlab R2015b
utc_offset =
-7
Or you can use datetime:
t1 = datetime(datestr(now),'TimeZone','local'); % can change local to whatever timezone interests you
dt=hours(tzoffset(t1)); % As a bonus, daylight savings time offset of 0 or 1 is also returned if you use [dt,dst]= (tzoffset(t1));
dt =
-7

类别

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