How to programmatically get the current/local time zone?
57 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
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
Réponse acceptée
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')
5 commentaires
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
This is what I get on my PC copy of Matlab:
>> dt = datetime('now', 'TimeZone', 'local');
>> tz = dt.TimeZone
tz =
'America/Chicago'
>>
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!