How to programmatically get the current/local time zone?

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 Commenti

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
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.

Accedi per commentare.

 Risposta accettata

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 Commenti

My goal is to obtain a datetime compatible string (e.g. "America/Chicago" or "America/New_York") for the timezone that the PC is currently in. Right now I have an app that logs data measurements and timestamps them by calling datetime(). But there is no "TimeZone" property associated with these timestamps--they are simply captured in the local time of the computer. Generally that's fine, but I want the ability to at a later date convert that datetime vector from whatever time zone those measurements were captured in to a new time zone of the user's choosing. If I have the proper TimeZone property defined at the time of the original call to datetime (e.g. "America/New_York", then I can later simply change that TimeZone property to a new user-selected timezone when plotting out my data in order to display the measurements in a new time zone. Does that make sense?
Create a sample datetime object with the 'local' time zone then extract its TimeZone property for later use.
dt = datetime('now', 'TimeZone', 'local');
tz = dt.TimeZone
tz = '+00:00'
Here's how you would use that information.
dt = datetime(2022, 12, 1, 11, 23, 45, ...
'TimeZone', tz, ...
'Format', 'd.MMM.yy HH:mm:ss Z')
dt = datetime
1.Dec.22 11:23:45 +0000
You can also assign time zone information to a datetime after it has been constructed if you want.
dt.TimeZone = 'America/New_York'
dt = datetime
1.Dec.22 06:23:45 -0500
If you're looking to translate that +00:00 into a location, there are a number of zones with UTCOffset equal to 0.
t = timezones;
t(t.UTCOffset == 0, :)
ans = 28×4 table
Name Area UTCOffset DSTOffset ________________________ __________ _________ _________ {'Africa/Abidjan' } Africa 0 0 {'Africa/Accra' } Africa 0 0 {'Africa/Bamako' } Africa 0 0 {'Africa/Banjul' } Africa 0 0 {'Africa/Bissau' } Africa 0 0 {'Africa/Conakry' } Africa 0 0 {'Africa/Dakar' } Africa 0 0 {'Africa/Freetown' } Africa 0 0 {'Africa/Lome' } Africa 0 0 {'Africa/Monrovia' } Africa 0 0 {'Africa/Nouakchott' } Africa 0 0 {'Africa/Ouagadougou' } Africa 0 0 {'Africa/Sao_Tome' } Africa 0 0 {'America/Danmarkshavn'} America 0 0 {'Antarctica/Troll' } Antarctica 0 2 {'Atlantic/Canary' } Atlantic 0 1
I'm not entirely sure where the system that MATLAB Answers uses to evaluate code is located but I'm guessing it is configured to use GMT or UTC from the Etc "area".
t = timezones('Etc');
t([1 end], :)
ans = 2×4 table
Name Area UTCOffset DSTOffset ___________ ____ _________ _________ {'Etc/GMT'} Etc 0 0 {'Etc/UTC'} Etc 0 0
Perfect! This was the magic code I was looking for:
dt = datetime('now', 'TimeZone', 'local');
tz = dt.TimeZone
tz = '+00:00'
I did not think to simply create a datetime variable with TimeZone set to 'local', then read that back as a property. On my local MATLAB instance, doing this indeed returns my actual local time zone in a compatible string as desired. Thanks!
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'
>>
@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'

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Prodotti

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by