Skip to content

Presenting tzlocal: A simple way to get your local timezone for pytz!

September 12, 2012

The Python timezone library pytz has one missing feature. There is no way to get the computers local timezone, unless you know the name of it, and figuring out the name is not always possible on some unix distros. And on windows, the timezone names are competely different from the ones pytz uses. dateutil.tz has support for it, but pytz does not.

tzlocal fixes that, by giving you a simple method get_localzone() that will return your local computers timezone as a tzinfo object, for use with pytz as normal.

Usage is simply:

>>> from tzlocal import get_localzone
>>> tz = get_localzone()

Any operating system with a zoneinfo file in /etc/localtime is supported, and also Windows. It needs widespread testing on many different distributions and different versions of Windows, so if you want to help, install it and test it on your computer!

About these ads
11 Comments
  1. merwok permalink

    Hey Lennart, cool idea! Maybe pytz developers would be open to include your function?

  2. barun permalink

    Hey Lennart,
    There are some timezones like America/New York which are not recognized by tzlocal and it gives Undefined Timezone Error or so. This happens in case of linux majorly as the tzlocal sends the timezone obtained from machine directly to pytz(in windows tzlocal first maps the obtained timezone in pytz format). Is there any workaround for this error on linux machines.

    • You’d have to give me more information to figure that one out. America/New York is definitely in the zoneinfo database and should be recognized.

  3. barun permalink

    I get the following traceback:-

    File “tzlocal/unix.py”, line 102, in get_localzone
    File “tzlocal/unix.py”, line 85, in _get_localzone
    File “pytz/__init__.py”, line 185, in timezone
    pytz.exceptions.UnknownTimeZoneError: ‘America/New York’

    I am trying to run my code on an RHEL machine.
    As per my debugging…
    tzlocal/unix.py picks up timezone from “etc/sysconfig/clock” file and sends it to pytz. But in the pytz database “New York” is present as “New_York”. and thus the mapping fails.

    Thanks for the help.

  4. Ah, I see. I’ll fix that in the next tzlocal release. Any space should be replaced with ‘_’ before lookup.

  5. barun permalink

    Yeah right. So, when is your next release expected?? Thanks…and till the time I will make local changes in the script.

  6. When I get time, which isn’t the nearest weeks at least.

  7. barun permalink

    Hi Lennart,
    I have made changes like….
    tz = pytz.timezone(tzenv.replace(‘ ‘, ‘_’))
    in place of pytz.timezone(tzenv) for all occurances in unix.py.

    If you have some time, can you please check if this would break something else. Or is it good to go??
    Thanks in advance.

  8. That should be good to go.

  9. merwok: I’m more than happy to put this in pytz, or we can just avoid the effort and wait until all this is integrated together in the Python standard library.

  10. merwok permalink

    Well, pytz will still be downloaded by millions of projects running on 2.7, 3.2 or 3.3.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,320 other followers

%d bloggers like this: