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
2017년 3월 28일
How do you have the time zone information? As a string of some sort that you need interpreted?
채택된 답변
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
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
댓글 수: 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!