~ubuntu-branches/ubuntu/quantal/python-tz/quantal

« back to all changes in this revision

Viewing changes to README.txt

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-10-27 07:29:54 UTC
  • mfrom: (1.1.9 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081027072954-y0tje6borovf4eva
Tags: 2008h-1
* New upstream version.
  - Fixes lookup errors with unknown timezones (error setting US/Pacific-New
    timezone. LP: #244681).
* debian/copyright: Update copyright year and download location.
* debian/control: Update homepage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
Amost all (over 540) of the Olson timezones are supported [*]_.
16
16
 
17
 
Note that if you perform date arithmetic on local times that cross
18
 
DST boundaries, the results may be in an incorrect timezone (ie.
19
 
subtract 1 minute from 2002-10-27 1:00 EST and you get 2002-10-27
20
 
0:59 EST instead of the correct 2002-10-27 1:59 EDT). This cannot
21
 
be resolved without modifying the Python datetime implementation.
22
 
However, these tzinfo classes provide a normalize() method which
23
 
allows you to correct these values.
 
17
Note that this library differs from the documented Python API for
 
18
tzinfo implementations; if you want to create local wallclock
 
19
times you need to use the localize() method documented in this
 
20
document. In addition, if you perform date arithmetic on local
 
21
times that cross DST boundaries, the results may be in an incorrect
 
22
timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get
 
23
2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT). A
 
24
normalize() method is provided to correct this. Unfortunatly these
 
25
issues cannot be resolved without modifying the Python datetime
 
26
implementation.
24
27
 
25
28
 
26
29
Installation
27
30
~~~~~~~~~~~~
28
31
 
29
 
This is a standard Python distutils distribution. To install the
30
 
package, run the following command as an administrative user::
 
32
This package can either be installed from a .egg file using setuptools,
 
33
or from the tarball using the standard Python distutils.
 
34
 
 
35
If you are installing from a tarball, run the following command as an
 
36
administrative user::
31
37
 
32
38
    python setup.py install
33
39
 
 
40
If you are installing using setuptools, you don't even need to download
 
41
anything as the latest version will be downloaded for you
 
42
from the Python package index::
 
43
 
 
44
    easy_install --upgrade pytz
 
45
 
 
46
If you already have the .egg file, you can use that too::
 
47
 
 
48
    easy_install pytz-2008g-py2.6.egg
 
49
 
34
50
 
35
51
Example & Usage
36
52
~~~~~~~~~~~~~~~
44
60
>>> eastern = timezone('US/Eastern')
45
61
>>> eastern.zone
46
62
'US/Eastern'
 
63
>>> amsterdam = timezone('Europe/Amsterdam')
47
64
>>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
48
65
 
 
66
This library only supports two ways of building a localized time. The
 
67
first is to use the .localize() method provided by the pytz library.
 
68
This is used to localize a naive datetime (datetime with no timezone
 
69
information):
 
70
 
 
71
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
 
72
>>> print loc_dt.strftime(fmt)
 
73
2002-10-27 06:00:00 EST-0500
 
74
 
 
75
The second way of building a localized time is by converting an existing
 
76
localized time using the standard .astimezone() method:
 
77
 
 
78
>>> ams_dt = loc_dt.astimezone(amsterdam)
 
79
>>> ams_dt.strftime(fmt)
 
80
'2002-10-27 12:00:00 CET+0100'
 
81
 
 
82
Unfortunately using the tzinfo argument of the standard datetime
 
83
constructors ''does not work'' with pytz for many timezones.
 
84
 
 
85
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
 
86
'2002-10-27 12:00:00 AMT+0020'
 
87
 
 
88
It is safe for timezones without daylight savings trasitions though, such
 
89
as UTC:
 
90
 
 
91
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=pytz.utc).strftime(fmt)
 
92
'2002-10-27 12:00:00 UTC+0000'
 
93
 
49
94
The preferred way of dealing with times is to always work in UTC,
50
95
converting to localtime only when generating output to be read
51
96
by humans.
98
143
>>> utc_dt2.strftime(fmt)
99
144
'2006-03-26 21:34:59 UTC+0000'
100
145
 
101
 
You can also take shortcuts when dealing with the UTC side of timezone
102
 
conversions. Normalize and localize are not really necessary because there
 
146
You can take shortcuts when dealing with the UTC side of timezone
 
147
conversions. Normalize and localize are not really necessary when there
103
148
are no daylight savings time transitions to deal with.
104
149
 
105
150
>>> utc_dt = datetime.utcfromtimestamp(1143408899).replace(tzinfo=utc)
140
185
not matter. However, if you are trying to schedule meetings with people
141
186
in different timezones or analyze log files it is not acceptable. 
142
187
 
143
 
The best and simplest solution is to stick with using UTC.  The pytz package
144
 
encourages using UTC for internal timezone representation by including a
145
 
special UTC implementation based on the standard Python reference 
146
 
implementation in the Python documentation.  This timezone unpickles to be
147
 
the same instance, and pickles to a relatively small size.  The UTC 
148
 
implementation can be obtained as pytz.utc, pytz.UTC, or 
149
 
pytz.timezone('UTC').  Note that this instance is not the same 
150
 
instance (or implementation) as other timezones with the same meaning 
151
 
(GMT, Greenwich, Universal, etc.).
 
188
The best and simplest solution is to stick with using UTC.  The pytz
 
189
package encourages using UTC for internal timezone representation by
 
190
including a special UTC implementation based on the standard Python
 
191
reference implementation in the Python documentation.  This timezone
 
192
unpickles to be the same instance, and pickles to a relatively small
 
193
size.  The UTC implementation can be obtained as pytz.utc, pytz.UTC,
 
194
or pytz.timezone('UTC').
152
195
 
153
196
>>> import pickle, pytz
154
197
>>> dt = datetime(2005, 3, 1, 14, 13, 21, tzinfo=utc)
166
209
True
167
210
>>> pytz.utc is pytz.UTC is pytz.timezone('UTC')
168
211
True
 
212
 
 
213
Note that this instance is not the same instance (or implementation) as
 
214
other timezones with the same meaning (GMT, Greenwich, Universal, etc.).
 
215
 
169
216
>>> utc is pytz.timezone('GMT')
170
217
False
171
218
 
172
219
If you insist on working with local times, this library provides a
173
 
facility for constructing them almost unambiguously.
 
220
facility for constructing them unambiguously:
174
221
 
175
222
>>> loc_dt = datetime(2002, 10, 27, 1, 30, 00)
176
223
>>> est_dt = eastern.localize(loc_dt, is_dst=True)
178
225
>>> print est_dt.strftime(fmt), '/', edt_dt.strftime(fmt)
179
226
2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500
180
227
 
181
 
Note that although this handles many cases, it is still not possible
 
228
If you pass None as the is_dst flag to localize(), pytz will refuse to
 
229
guess and raise exceptions if you try to build ambiguous or non-existent
 
230
times.
 
231
 
 
232
For example, 1:30am on 27th Oct 2002 happened twice in the US/Eastern
 
233
timezone when the clocks where put back at the end of Daylight Savings
 
234
Time:
 
235
 
 
236
>>> eastern.localize(datetime(2002, 10, 27, 1, 30, 00), is_dst=None)
 
237
Traceback (most recent call last):
 
238
...
 
239
AmbiguousTimeError: 2002-10-27 01:30:00
 
240
 
 
241
Similarly, 2:30am on 7th April 2002 never happened at all in the
 
242
US/Eastern timezone, as the clock where put forward at 2:00am skipping
 
243
the entire hour:
 
244
 
 
245
>>> eastern.localize(datetime(2002, 4, 7, 2, 30, 00), is_dst=None)
 
246
Traceback (most recent call last):
 
247
...
 
248
NonExistentTimeError: 2002-04-07 02:30:00
 
249
 
 
250
Both of these exceptions share a common base class to make error handling
 
251
easier:
 
252
 
 
253
>>> isinstance(pytz.AmbiguousTimeError(), pytz.InvalidTimeError)
 
254
True
 
255
>>> isinstance(pytz.NonExistentTimeError(), pytz.InvalidTimeError)
 
256
True
 
257
 
 
258
Although localize() handles many cases, it is still not possible
182
259
to handle all. In cases where countries change their timezone definitions,
183
260
cases like the end-of-daylight-savings-time occur with no way of resolving
184
261
the ambiguity. For example, in 1915 Warsaw switched from Warsaw time to
185
 
Central European time. So at the stroke of midnight on August 4th 1915
186
 
the clocks were wound back 24 minutes creating a ambiguous time period
 
262
Central European time. So at the stroke of midnight on August 5th 1915
 
263
the clocks were wound back 24 minutes creating an ambiguous time period
187
264
that cannot be specified without referring to the timezone abbreviation
188
 
or the actual UTC offset.
 
265
or the actual UTC offset. In this case midnight happened twice, neither
 
266
time during a daylight savings time period:
 
267
 
 
268
>>> warsaw = pytz.timezone('Europe/Warsaw')
 
269
>>> loc_dt1 = warsaw.localize(datetime(1915, 8, 4, 23, 59, 59), is_dst=False)
 
270
>>> loc_dt1.strftime(fmt)
 
271
'1915-08-04 23:59:59 WMT+0124'
 
272
>>> loc_dt2 = warsaw.localize(datetime(1915, 8, 5, 00, 00, 00), is_dst=False)
 
273
>>> loc_dt2.strftime(fmt)
 
274
'1915-08-05 00:00:00 CET+0100'
 
275
>>> str(loc_dt2 - loc_dt1)
 
276
'0:24:01'
 
277
 
 
278
The only way of creating a time during the missing 24 minutes is converting
 
279
from another time - because neither of the timezones involved where in
 
280
daylight savings mode the API simply provides no way to express it:
 
281
 
 
282
>>> utc_dt = datetime(1915, 8, 4, 22, 36, tzinfo=pytz.utc)
 
283
>>> utc_dt.astimezone(warsaw).strftime(fmt)
 
284
'1915-08-04 23:36:00 CET+0100'
189
285
 
190
286
The 'Standard' Python way of handling all these ambiguities is not to,
191
287
such as demonstrated in this example using the US/Eastern timezone
214
310
'2002-10-27 06:30:00+00:00'
215
311
 
216
312
 
 
313
Country Information
 
314
~~~~~~~~~~~~~~~~~~~
 
315
 
 
316
A mechanism is provided to access the timezones commonly in use
 
317
for a particular country, looked up using the ISO 3166 country code.
 
318
It returns a list of strings that can be used to retrieve the relevant
 
319
tzinfo instance using `pytz.timezone()`:
 
320
 
 
321
>>> pytz.country_timezones['nz']
 
322
['Pacific/Auckland', 'Pacific/Chatham']
 
323
 
 
324
The Olson database comes with a ISO 3166 country code to English country
 
325
name mapping that pytz exposes as a dictionary:
 
326
 
 
327
>>> pytz.country_names['nz']
 
328
'New Zealand'
 
329
 
 
330
 
217
331
What is UTC
218
332
~~~~~~~~~~~
219
333
 
220
 
`UTC` is Universal Time, formerly known as Greenwich Mean Time or GMT.
221
 
All other timezones are given as offsets from UTC. No daylight savings
222
 
time occurs in UTC, making it a useful timezone to perform date arithmetic
223
 
without worrying about the confusion and ambiguities caused by daylight
224
 
savings time transitions, your country changing its timezone, or mobile
225
 
computers that move roam through multiple timezones.
 
334
`UTC` is Universal Time, also known as Greenwich Mean Time or GMT
 
335
in the United Kingdom. All other timezones are given as offsets from
 
336
UTC. No daylight savings time occurs in UTC, making it a useful timezone
 
337
to perform date arithmetic without worrying about the confusion and
 
338
ambiguities caused by daylight savings time transitions, your country
 
339
changing its timezone, or mobile computers that move roam through
 
340
multiple timezones.
226
341
 
227
342
 
228
343
Helpers
239
354
True
240
355
 
241
356
`common_timezones` is a list of useful, current timezones. It doesn't
242
 
contain deprecated zones or historical zones. It is also a sequence of
243
 
strings.
 
357
contain deprecated zones or historical zones, except for a few I've
 
358
deemed in common usage, such as US/Eastern (open a bug report if you
 
359
think other timezones are deserving of being included here).It is also
 
360
a sequence of strings.
244
361
 
245
362
>>> from pytz import common_timezones
246
363
>>> len(common_timezones) < len(all_timezones)
247
364
True
248
365
>>> 'Etc/Greenwich' in common_timezones
249
366
False
 
367
>>> 'US/Eastern' in common_timezones
 
368
True
 
369
>>> 'Australia/Melbourne' in common_timezones
 
370
True
 
371
>>> 'US/Pacific-New' in all_timezones
 
372
True
 
373
>>> 'US/Pacific-New' in common_timezones
 
374
False
 
375
 
 
376
Both common_timezones and all_timezones are alphabetically sorted:
 
377
 
 
378
>>> common_timezones_dupe = common_timezones[:]
 
379
>>> common_timezones_dupe.sort()
 
380
>>> common_timezones == common_timezones_dupe
 
381
True
 
382
>>> all_timezones_dupe = all_timezones[:]
 
383
>>> all_timezones_dupe.sort()
 
384
>>> all_timezones == all_timezones_dupe
 
385
True
 
386
 
 
387
`all_timezones` and `common_timezones` are also available as sets.
 
388
 
 
389
>>> from pytz import all_timezones_set, common_timezones_set
 
390
>>> 'US/Eastern' in all_timezones_set
 
391
True
 
392
>>> 'US/Eastern' in common_timezones_set
 
393
True
 
394
>>> 'Australia/Victoria' in common_timezones_set
 
395
False
250
396
 
251
397
You can also retrieve lists of timezones used by particular countries
252
398
using the `country_timezones()` method. It requires an ISO-3166 two letter
272
418
Latest Versions
273
419
~~~~~~~~~~~~~~~
274
420
 
275
 
This package will be updated after releases of the Olson timezone database.
276
 
The latest version can be downloaded from the Python Cheeseshop_ or
277
 
Sourceforge_. The code that is used to generate this distribution is
278
 
available using the Bazaar_ revision control system using::
279
 
 
280
 
    bzr branch http://bazaar.launchpad.net/~stub/pytz/devel
281
 
 
282
 
.. _Cheeseshop: http://cheeseshop.python.org/pypi/pytz/
283
 
.. _Sourceforge: http://sourceforge.net/projects/pytz/
 
421
This package will be updated after releases of the Olson timezone
 
422
database.  The latest version can be downloaded from the Python Package
 
423
Index (PyPI_).  The code that is used to generate this distribution is
 
424
hosted on launchpad.net and available using the Bazaar_ revision control
 
425
system using::
 
426
 
 
427
    bzr branch lp:pytz
 
428
 
 
429
.. _PyPI: http://cheeseshop.python.org/pypi/pytz/
284
430
.. _Bazaar: http://bazaar-vcs.org/
285
431
 
286
432
Bugs, Feature Requests & Patches