How to programmatically get the current/local time zone?
53 ビュー (過去 30 日間)
古いコメントを表示
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
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
採用された回答
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')
5 件のコメント
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
This is what I get on my PC copy of Matlab:
>> dt = datetime('now', 'TimeZone', 'local');
>> tz = dt.TimeZone
tz =
'America/Chicago'
>>
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!