Get the Olson TZ name for the local timezone?

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C's localtime call?,Thanks for contributing an answer to Stack Overflow!, Meta Stack Overflow ,So you will need to get all the possible names for the local time zone, and then select the name you like.

This is kind of cheating, I know, but getting from '/etc/localtime' doesn't work for you? Like following:

>>>
import os
   >>>
   '/'.join(os.readlink('/etc/localtime').split('/')[-2: ])
'Australia/Sydney'

Edit: I liked @A.H.'s idea, in case '/etc/localtime' isn't a symlink. Translating that into Python:

#!/usr/bin/env python

from hashlib
import sha224
import os

def get_current_olsonname():
   tzfile = open('/etc/localtime')
tzfile_digest = sha224(tzfile.read()).hexdigest()
tzfile.close()

for root, dirs, filenames in os.walk("/usr/share/zoneinfo/"):
   for filename in filenames:
   fullname = os.path.join(root, filename)
f = open(fullname)
digest = sha224(f.read()).hexdigest()
if digest == tzfile_digest:
   return '/'.join((fullname.split('/'))[-2: ])
f.close()
return None

if __name__ == '__main__':
   print get_current_olsonname()
load more v
88%

How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C's localtime call?,This is the value overridden via TZ, by symlinking /etc/localtime, or setting a TIMEZONE variable in time-related system configuration files.,there can be more than one timezone with the valid offset at the specified date and time (then you'll have to choose one, based on any arbitrary criteria).,Always prefer to use IANA timezones names (always in the format Continent/City, like America/Sao_Paulo or Europe/Berlin). If Asia/Kolkata is not what you need, check this list to find the one that suits best for your case.

I think best bet is to go thru all pytz timezones and check which one matches local timezone, each pytz timezone object contains info about utcoffset and tzname like CDT, EST, same info about local time can be obtained from time.timezone/altzone and time.tzname, and I think that is enough to correctly match local timezone in pytz database e.g.

import time
import pytz
import datetime

local_names = []
if time.daylight:
   local_offset = time.altzone
localtz = time.tzname[1]
else :
   local_offset = time.timezone
localtz = time.tzname[0]

local_offset = datetime.timedelta(seconds = -local_offset)

for name in pytz.all_timezones:
   timezone = pytz.timezone(name)
if not hasattr(timezone, '_tzinfos'):
   continue #skip,
      if some timezone doesn 't have info
# go thru tzinfo and see
if short name like EDT and offset matches
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.iteritems():
   if utcoffset == local_offset and tzname == localtz:
   local_names.append(name)

print local_names
load more v
72%

This will get you the time zone name, according to what's in the TZ variable, or localtime file if unset:,I think best bet is to go thru all pytz timezones and check which one matches local timezone, each pytz timezone object contains info about utcoffset and tzname like CDT, EST, same info about local time can be obtained from time.timezone/altzone and time.tzname, and I think that is enough to correctly match local timezone in pytz database e.g.,So you will need to get all the possible names for the local time zone, and then select the name you like.,Here it is interpreting niave datetimes (as would be returned by a database query) in the local timezone.

I think best bet is to go thru all pytz timezones and check which one matches local timezone, each pytz timezone object contains info about utcoffset and tzname like CDT, EST, same info about local time can be obtained from time.timezone/altzone and time.tzname, and I think that is enough to correctly match local timezone in pytz database e.g.

import time
import pytz
import datetime

local_names = []
if time.daylight:
   local_offset = time.altzone
localtz = time.tzname[1]
else :
   local_offset = time.timezone
localtz = time.tzname[0]

local_offset = datetime.timedelta(seconds = -local_offset)

for name in pytz.all_timezones:
   timezone = pytz.timezone(name)
if not hasattr(timezone, '_tzinfos'):
   continue #skip,
      if some timezone doesn 't have info
# go thru tzinfo and see
if short name like EDT and offset matches
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.iteritems():
   if utcoffset == local_offset and tzname == localtz:
   local_names.append(name)

print local_names
load more v
65%

A literary appreciation of the Olson/Zoneinfo/tz database comments on the database's style.,Web sites using recent versions of the tz database,Commentary on the tz database,Other time zone databases

The following shell commands download the latest release's two tarballs to a GNU/Linux or similar host.

mkdir tzdb
cd tzdb
wget https: //www.iana.org/time-zones/repository/tzcode-latest.tar.gz
   wget https: //www.iana.org/time-zones/repository/tzdata-latest.tar.gz
   gzip - dc tzcode - latest.tar.gz | tar - xf -
   gzip - dc tzdata - latest.tar.gz | tar - xf -
load more v
75%

OlsonNames returns a character vector, see the examples for typical cases. It may have an attribute "Version", something like "2020a". ,Setting TZDIR is also supported by the native services on some OSes, e.g. Linux using glibc except in secure modes. ,This section is of background interest for users of a Unix-alike, but may help if an NA value is returned unexpectedly. ,Note that except where replaced, the operation of time zones is an OS service, and even where replaced a third-party database is used and can be updated (see the section on ‘Time zone names’). Incorrect results will never be an R issue, so please ensure that you have the courtesy not to blame R for them.

1
2
3
load more v
40%

The following examples represent some of the customized POSIX formats:,The TZ environment variable is used to represent time zone information. The value of TZ can be specified either in the POSIX or Olson format.,CST6CDT is the name of the time zone,Understanding the POSIX format

TZ = CST6CDT, M3 .2 .0 / 2: 00: 00, M11 .1 .0 / 2: 00: 00
load more v
22%

Tcl, mentioned above, also contains a tz binary file reader.,Alternatively, the following shell commands download the same release in a single-tarball format containing extra data useful for regression testing:,Geographical boundaries between time zone regions are available from several geolocation services and other sources.,Time Zone Master is a Microsoft Windows clock program that can automatically download, compile and use tz releases. The Basic version is free.

The following shell commands download the latest release's two tarballs to a GNU/Linux or similar host.

mkdir tzdb
cd tzdb
wget--retr - symlinks 'ftp://ftp.iana.org/tz/tz*-latest.tar.gz'
gzip - dc tzcode - latest.tar.gz | tar - xf -
   gzip - dc tzdata - latest.tar.gz | tar - xf -
load more v

Other "undefined-undefined" queries related to "Get the Olson TZ name for the local timezone?"