필터 지우기
필터 지우기

How to programmatically get the current/local time zone?

조회 수: 62 (최근 30일)
matlabuser77
matlabuser77 2022년 11월 30일
댓글: Steven Lord 2022년 12월 1일
I want to get the local time zone of the computer that is running my MATLAB function/script. For example, 'America/New_York' or 'America/Chicago' etc. (I want it in that specific format so that I can use it with datetime arrays.)
Calling datetime('now') does not return a time zone.
Is there a way to get the time zone from within MATLAB? Or to somehow read the time zone from Windows itself?
  댓글 수: 2
Les Beckham
Les Beckham 2022년 11월 30일
On Windows, this will get the current time zone, though not in the format you are looking for:
[~, tz] = system('tzutil /g')
This will return, for example:
Central Standard Time
instead of America/Chicago
matlabuser77
matlabuser77 2022년 12월 1일
Thanks! That is helpful. Is there a way to convert the returned "tz" variable string into a string compatible with the "TimeZone" property of a datetime vector or array?
I'd like to check the current time zone of the computer, then use that to set the TimeZone property of a datetime array that I am creating in tandem with some data that I am capturing. Right now I am using datetime() to timestamp those measurements, but there is no TimeZone defined for the measurements...they are just captured in the local time zone of the computer, which I don't necessarily know. I'd like to define the TimeZone property programatically so that I can later convert to different time zones when plotting out my data.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2022년 11월 30일
Do you want to create a datetime in the current time zone or do you want to know the current time zone? Those are two different tasks, and if you want the former use the 'local' value for the TimeZone option when you construct the datetime object as shown on this documentation page.
t = datetime('now', TimeZone = 'local', Format = 'd-MMM-y HH:mm:ss Z')
t = datetime
30-Nov-2022 19:56:31 +0000
  댓글 수: 5
Les Beckham
Les Beckham 2022년 12월 1일
Interestingly, when I try @Steven Lord's suggestion on my PC instead of online, I get what the OP originally asked for. This is what I get online.
dt = datetime('now', 'TimeZone', 'local');
tz = dt.TimeZone
tz = '+00:00'
This is what I get on my PC copy of Matlab:
>> dt = datetime('now', 'TimeZone', 'local');
>> tz = dt.TimeZone
tz =
'America/Chicago'
>>
Steven Lord
Steven Lord 2022년 12월 1일
@Les Beckham Yes. Both '+00:00' and 'America/Chicago' are valid values for the TimeZone property of a datetime array. From the description of that property the former is "An ISO 8601 character vector of the form +HH:mm or -HH:mm; for example, '+01:00', to specify a time zone that is a fixed offset from UTC." while the latter is "The name of a time zone region from the IANA Time Zone Database"
Ah, taking a closer look at the documentation there's another read-only property of a datetime array that is more directly applicable to the original question.
dt = datetime('now');
dt.SystemTimeZone
ans = 'Etc/UTC'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by