Determine UTC Offset for a given Time Zone?

5 vues (au cours des 30 derniers jours)
peaksix
peaksix le 28 Mar 2017
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 commentaire
James Tursa
James Tursa le 28 Mar 2017
How do you have the time zone information? As a string of some sort that you need interpreted?

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 29 Mar 2017
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 commentaire
peaksix
peaksix le 29 Mar 2017
Thanks, this worked perfectly.

Connectez-vous pour commenter.

Plus de réponses (1)

Jerome Blaha
Jerome Blaha le 25 Mai 2023
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

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by