How to programmatically get the current/local time zone?

64 ビュー (過去 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 Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by