How to programmatically get the current/local time zone?

57 vues (au cours des 30 derniers jours)
matlabuser77
matlabuser77 le 30 Nov 2022
Commenté : Steven Lord le 1 Déc 2022
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 commentaires
Les Beckham
Les Beckham le 30 Nov 2022
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 le 1 Déc 2022
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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 30 Nov 2022
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 commentaires
Les Beckham
Les Beckham le 1 Déc 2022
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 le 1 Déc 2022
@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'

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by