@@ -7,7 +7,7 @@ Introduction
77~~~~~~~~~~~~
88
99pytz brings the Olson tz database into Python. This library allows
10- accurate and cross platform timezone calculations using Python 2.3
10+ accurate and cross platform timezone calculations using Python 2.4
1111or higher. It also solves the issue of ambiguous times at the end
1212of daylight savings, which you can read more about in the Python
1313Library Reference (``datetime.tzinfo``).
@@ -72,7 +72,7 @@ This is used to localize a naive datetime (datetime with no timezone
7272information):
7373
7474>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
75- >>> print loc_dt.strftime(fmt)
75+ >>> print( loc_dt.strftime(fmt) )
76762002-10-27 06:00:00 EST-0500
7777
7878The second way of building a localized time is by converting an existing
@@ -209,7 +209,7 @@ datetime.timedelta(0)
209209'NST'
210210
211211If ``is_dst`` is not specified, ambiguous timestamps will raise
212- an ``AmbiguousTimeError`` exception.
212+ an ``pytz.exceptions. AmbiguousTimeError`` exception.
213213
214214>>> tz.utcoffset(normal)
215215datetime.timedelta(-1, 77400)
@@ -218,18 +218,22 @@ datetime.timedelta(0, 3600)
218218>>> tz.tzname(normal)
219219'NDT'
220220
221- >>> tz.utcoffset(ambiguous)
222- Traceback (most recent call last):
223- [...]
224- AmbiguousTimeError: 2009-10-31 23:30:00
225- >>> tz.dst(ambiguous)
226- Traceback (most recent call last):
227- [...]
228- AmbiguousTimeError: 2009-10-31 23:30:00
229- >>> tz.tzname(ambiguous)
230- Traceback (most recent call last):
231- [...]
232- AmbiguousTimeError: 2009-10-31 23:30:00
221+ >>> import pytz.exceptions
222+ >>> try:
223+ ... tz.utcoffset(ambiguous)
224+ ... except pytz.exceptions.AmbiguousTimeError:
225+ ... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
226+ pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
227+ >>> try:
228+ ... tz.dst(ambiguous)
229+ ... except pytz.exceptions.AmbiguousTimeError:
230+ ... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
231+ pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
232+ >>> try:
233+ ... tz.tzname(ambiguous)
234+ ... except pytz.exceptions.AmbiguousTimeError:
235+ ... print('pytz.exceptions.AmbiguousTimeError: %s' % ambiguous)
236+ pytz.exceptions.AmbiguousTimeError: 2009-10-31 23:30:00
233237
234238
235239Problems with Localtime
@@ -261,18 +265,19 @@ in different timezones or analyze log files it is not acceptable.
261265The best and simplest solution is to stick with using UTC. The pytz
262266package encourages using UTC for internal timezone representation by
263267including a special UTC implementation based on the standard Python
264- reference implementation in the Python documentation. This timezone
265- unpickles to be the same instance, and pickles to a relatively small
266- size. The UTC implementation can be obtained as pytz.utc, pytz.UTC,
267- or pytz.timezone('UTC').
268+ reference implementation in the Python documentation.
269+
270+ The UTC timezone unpickles to be the same instance, and pickles to a
271+ smaller size than other pytz tzinfo instances. The UTC implementation
272+ can be obtained as pytz.utc, pytz.UTC, or pytz.timezone('UTC').
268273
269274>>> import pickle, pytz
270275>>> dt = datetime(2005, 3, 1, 14, 13, 21, tzinfo=utc)
271276>>> naive = dt.replace(tzinfo=None)
272277>>> p = pickle.dumps(dt, 1)
273278>>> naive_p = pickle.dumps(naive, 1)
274- >>> len(p), len(naive_p), len(p) - len(naive_p)
275- (60, 43, 17)
279+ >>> len(p) - len(naive_p)
280+ 17
276281>>> new = pickle.loads(p)
277282>>> new == dt
278283True
@@ -295,7 +300,7 @@ facility for constructing them unambiguously:
295300>>> loc_dt = datetime(2002, 10, 27, 1, 30, 00)
296301>>> est_dt = eastern.localize(loc_dt, is_dst=True)
297302>>> edt_dt = eastern.localize(loc_dt, is_dst=False)
298- >>> print est_dt.strftime(fmt), '/', edt_dt.strftime(fmt)
303+ >>> print( est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt) )
2993042002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500
300305
301306If you pass None as the is_dst flag to localize(), pytz will refuse to
@@ -306,19 +311,23 @@ For example, 1:30am on 27th Oct 2002 happened twice in the US/Eastern
306311timezone when the clocks where put back at the end of Daylight Savings
307312Time:
308313
309- >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 00), is_dst=None)
310- Traceback (most recent call last):
311- ...
312- AmbiguousTimeError: 2002-10-27 01:30:00
314+ >>> dt = datetime(2002, 10, 27, 1, 30, 00)
315+ >>> try:
316+ ... eastern.localize(dt, is_dst=None)
317+ ... except pytz.exceptions.AmbiguousTimeError:
318+ ... print('pytz.exceptions.AmbiguousTimeError: %s' % dt)
319+ pytz.exceptions.AmbiguousTimeError: 2002-10-27 01:30:00
313320
314321Similarly, 2:30am on 7th April 2002 never happened at all in the
315322US/Eastern timezone, as the clocks where put forward at 2:00am skipping
316323the entire hour:
317324
318- >>> eastern.localize(datetime(2002, 4, 7, 2, 30, 00), is_dst=None)
319- Traceback (most recent call last):
320- ...
321- NonExistentTimeError: 2002-04-07 02:30:00
325+ >>> dt = datetime(2002, 4, 7, 2, 30, 00)
326+ >>> try:
327+ ... eastern.localize(dt, is_dst=None)
328+ ... except pytz.exceptions.NonExistentTimeError:
329+ ... print('pytz.exceptions.NonExistentTimeError: %s' % dt)
330+ pytz.exceptions.NonExistentTimeError: 2002-04-07 02:30:00
322331
323332Both of these exceptions share a common base class to make error handling
324333easier:
@@ -393,14 +402,14 @@ for a particular country, looked up using the ISO 3166 country code.
393402It returns a list of strings that can be used to retrieve the relevant
394403tzinfo instance using ``pytz.timezone()``:
395404
396- >>> pytz.country_timezones['nz']
397- [' Pacific/Auckland', ' Pacific/Chatham']
405+ >>> print(' '.join( pytz.country_timezones['nz']))
406+ Pacific/Auckland Pacific/Chatham
398407
399408The Olson database comes with a ISO 3166 country code to English country
400409name mapping that pytz exposes as a dictionary:
401410
402- >>> pytz.country_names['nz']
403- ' New Zealand'
411+ >>> print( pytz.country_names['nz'])
412+ New Zealand
404413
405414
406415What is UTC
@@ -478,10 +487,10 @@ using the ``country_timezones()`` function. It requires an ISO-3166
478487two letter country code.
479488
480489>>> from pytz import country_timezones
481- >>> country_timezones('ch')
482- [' Europe/Zurich']
483- >>> country_timezones('CH')
484- [' Europe/Zurich']
490+ >>> print(' '.join( country_timezones('ch')) )
491+ Europe/Zurich
492+ >>> print(' '.join( country_timezones('CH')) )
493+ Europe/Zurich
485494
486495
487496License
@@ -541,3 +550,4 @@ Contact
541550
542551543552
553+
0 commit comments