ANN: pytz 2004b.2 - Python world timezone library
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Changes for this release:
- Improved localtime handling, and added a localize() method enabling
correct creation of local times.
pytz - World Timezone Definitions for Python
============================================
:Author: Stuart Bishop
from datetime import datetime, timedelta from pytz import timezone utc = timezone('UTC') eastern = timezone('US/Eastern') fmt = '%Y-%m-%d %H:%M:%S %Z%z'
The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans.
utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc) loc_dt = utc_dt.astimezone(eastern) loc_dt.strftime(fmt) '2002-10-27 01:00:00 EST-0500'
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.
before = loc_dt - timedelta(minutes=10) before.strftime(fmt) '2002-10-27 00:50:00 EST-0500' eastern.normalize(before).strftime(fmt) '2002-10-27 01:50:00 EDT-0400' after = eastern.normalize(before + timedelta(minutes=20)) after.strftime(fmt) '2002-10-27 01:10:00 EST-0500'
Creating localtimes 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)
dt = datetime(2002, 10, 27, 1, 30, 0) dt1 = eastern.localize(dt, is_dst=True) dt1.strftime(fmt) '2002-10-27 01:30:00 EDT-0400' dt2 = eastern.localize(dt, is_dst=False) dt2.strftime(fmt) '2002-10-27 01:30:00 EST-0500'
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.
loc_dt = datetime(2002, 10, 27, 1, 30, 00, tzinfo=eastern) loc_dt.strftime(fmt) '2002-10-27 01:30:00 EST-0500'
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. If you insist on working with local times, this library provides a facility for constructing them almost unambiguously
loc_dt = datetime(2002, 10, 27, 1, 30, 00) est_dt = eastern.localize(loc_dt, is_dst=True) edt_dt = eastern.localize(loc_dt, is_dst=False) print est_dt.strftime(fmt), '/', edt_dt.strftime(fmt) 2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500
Note that although this 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 4th 1915
the clocks were wound back 24 minutes creating a ambiguous time period
that cannot be specified without referring to the timezone abbreviation
or the actual UTC offset.
What is UTC
~~~~~~~~~~~
`UTC` is Universal Time, formerly known as Greenwich Mean Time or GMT.
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.
License
~~~~~~~
BSD style license. 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 Olsen timezone
database.
The latest version can be downloaded from Sourceforge_. The code that
is used to generate this distribution is available in the Sourceforge_
project's CVS repository.
.. _Sourceforge: http://sourceforge.net/projects/pytz/
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.
Further Reading
~~~~~~~~~~~~~~~
More info than you want to know about timezones::
http://www.twinsun.com/tz/tz-link.htm
Contact
~~~~~~~
Stuart Bishop
participants (1)
-
Stuart Bishop