Not Logged In

pytz 2013b

Introduction

pytz brings the Olson tz database into Python. This library allows
accurate and cross platform timezone calculations using Python 2.4
or higher. It also solves the issue of ambiguous times at the end
of daylight savings, which you can read more about in the Python
Library Reference (datetime.tzinfo).

Almost all of the Olson timezones are supported.

Note that this library differs from the documented Python API for
tzinfo implementations; if you want to create local wallclock
times you need to use the localize() method documented in this
document. In addition, if you perform date arithmetic on local
times that cross DST boundaries, the result may be in an incorrect
timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get
2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT). A
normalize() method is provided to correct this. Unfortunately these
issues cannot be resolved without modifying the Python datetime
implementation.

Installation

This package can either be installed from a .egg file using setuptools,
or from the tarball using the standard Python distutils.

If you are installing from a tarball, run the following command as an
administrative user:

python setup.py install

If you are installing using setuptools, you don’t even need to download
anything as the latest version will be downloaded for you
from the Python package index:

This library only supports two ways of building a localized time. The
first is to use the localize() method provided by the pytz library.
This is used to localize a naive datetime (datetime with no timezone
information):

This library also allows you to do date arithmetic using local
times, although it is more complicated than working in UTC as you
need to use the normalize() method to handle daylight savings time
and other timezone transitions. In this example, loc_dt is set
to the instant when daylight savings time ends in the US/Eastern
timezone.

Creating local times is also tricky, and the reason why working with
local times is not recommended. Unfortunately, you cannot just pass
a tzinfo argument when constructing a datetime (see the next
section for more details)

Problems with Localtime

The major problem we have to deal with is that certain datetimes
may occur twice in a year. For example, in the US/Eastern timezone
on the last Sunday morning in October, the following sequence
happens:

01:00 EDT occurs

1 hour later, instead of 2:00am the clock is turned back 1 hour
and 01:00 happens again (this time 01:00 EST)

In fact, every instant between 01:00 and 02:00 occurs twice. This means
that if you try and create a time in the ‘US/Eastern’ timezone using
the standard datetime syntax, there is no way to specify if you meant
before of after the end-of-daylight-savings-time transition.

As you can see, the system has chosen one for you and there is a 50%
chance of it being out by one hour. For some applications, this does
not matter. However, if you are trying to schedule meetings with people
in different timezones or analyze log files it is not acceptable.

The best and simplest solution is to stick with using UTC. The pytz
package encourages using UTC for internal timezone representation by
including a special UTC implementation based on the standard Python
reference implementation in the Python documentation.

The UTC timezone unpickles to be the same instance, and pickles to a
smaller size than other pytz tzinfo instances. The UTC implementation
can be obtained as pytz.utc, pytz.UTC, or pytz.timezone(‘UTC’).

Although localize() handles many cases, it is still not possible
to handle all. In cases where countries change their timezone definitions,
cases like the end-of-daylight-savings-time occur with no way of resolving
the ambiguity. For example, in 1915 Warsaw switched from Warsaw time to
Central European time. So at the stroke of midnight on August 5th 1915
the clocks were wound back 24 minutes creating an ambiguous time period
that cannot be specified without referring to the timezone abbreviation
or the actual UTC offset. In this case midnight happened twice, neither
time during a daylight savings time period:

The only way of creating a time during the missing 24 minutes is
converting from another timezone - because neither of the timezones
involved where in daylight savings mode the API simply provides no way
to express it:

The standard Python way of handling all these ambiguities is not to
handle them, such as demonstrated in this example using the US/Eastern
timezone definition from the Python documentation (Note that this
implementation only works for dates between 1987 and 2006 - it is
included for tests only!):

Notice the first two results? At first glance you might think they are
correct, but taking the UTC offset into account you find that they are
actually two hours appart instead of the 1 hour we asked for.

Country Information

A mechanism is provided to access the timezones commonly in use
for a particular country, looked up using the ISO 3166 country code.
It returns a list of strings that can be used to retrieve the relevant
tzinfo instance using pytz.timezone():

The Olson database comes with a ISO 3166 country code to English country
name mapping that pytz exposes as a dictionary:

>>> print(pytz.country_names['nz'])
New Zealand

What is UTC

‘UTC’ is Universal Time, also known as Greenwich Mean Time or GMT
in the United Kingdom. All other timezones are given as offsets from
UTC. No daylight savings time occurs in UTC, making it a useful timezone
to perform date arithmetic without worrying about the confusion and
ambiguities caused by daylight savings time transitions, your country
changing its timezone, or mobile computers that move roam through
multiple timezones.

Helpers

There are two lists of timezones provided.

all_timezones is the exhaustive list of the timezone names that can
be used.

common_timezones is a list of useful, current timezones. It doesn’t
contain deprecated zones or historical zones, except for a few I’ve
deemed in common usage, such as US/Eastern (open a bug report if you
think other timezones are deserving of being included here). It is also
a sequence of strings.

License

This code is also available as part of Zope 3 under the Zope Public
License, Version 2.1 (ZPL).

I’m happy to relicense this code if necessary for inclusion in other
open source projects.

Latest Versions

This package will be updated after releases of the Olson timezone
database. The latest version can be downloaded from the Python Package
Index. The code that is used
to generate this distribution is hosted on launchpad.net and available
using the Bazaar version control system
using:

Bugs, Feature Requests & Patches

Issues & Limitations

Offsets from UTC are rounded to the nearest whole minute, so timezones
such as Europe/Amsterdam pre 1937 will be up to 30 seconds out. This
is a limitation of the Python datetime library.

If you think a timezone definition is incorrect, I probably can’t fix
it. pytz is a direct translation of the Olson timezone database, and
changes to the timezone definitions need to be made to this source.
If you find errors they should be reported to the time zone mailing
list, linked from http://www.twinsun.com/tz/tz-link.htm