| | |
- datetime.tzinfo(__builtin__.object)
-
- BaseTzInfo
-
- DstTzInfo
- StaticTzInfo
- exceptions.Exception(exceptions.BaseException)
-
- AmbiguousTimeError
class DstTzInfo(BaseTzInfo) |
| |
A timezone that has a variable offset from UTC
The offset might change if daylight savings time comes into effect,
or at a point in history when the region decides to change their
timezone definition. |
| |
- Method resolution order:
- DstTzInfo
- BaseTzInfo
- datetime.tzinfo
- __builtin__.object
Methods defined here:
- __init__(self, _inf=None, _tzinfos=None)
- __repr__(self)
- dst(self, dt)
- See datetime.tzinfo.dst
- fromutc(self, dt)
- See datetime.tzinfo.fromutc
- localize(self, dt, is_dst=False)
- Convert naive time to local time.
This method should be used to construct localtimes, rather
than passing a tzinfo argument to a datetime constructor.
is_dst is used to determine the correct timezone in the ambigous
period at the end of daylight savings time.
>>> from pytz import timezone
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
>>> amdam = timezone('Europe/Amsterdam')
>>> dt = datetime(2004, 10, 31, 2, 0, 0)
>>> loc_dt1 = amdam.localize(dt, is_dst=True)
>>> loc_dt2 = amdam.localize(dt, is_dst=False)
>>> loc_dt1.strftime(fmt)
'2004-10-31 02:00:00 CEST (+0200)'
>>> loc_dt2.strftime(fmt)
'2004-10-31 02:00:00 CET (+0100)'
>>> str(loc_dt2 - loc_dt1)
'1:00:00'
Use is_dst=None to raise an AmbiguousTimeError for ambiguous
times at the end of daylight savings
>>> try:
... loc_dt1 = amdam.localize(dt, is_dst=None)
... except AmbiguousTimeError:
... print 'Oops'
Oops
>>> loc_dt1 = amdam.localize(dt, is_dst=None)
Traceback (most recent call last):
[...]
AmbiguousTimeError: 2004-10-31 02:00:00
is_dst defaults to False
>>> amdam.localize(dt) == amdam.localize(dt, False)
True
- normalize(self, dt)
- Correct the timezone information on the given datetime
If date arithmetic crosses DST boundaries, the tzinfo
is not magically adjusted. This method normalizes the
tzinfo to the correct one.
To test, first we need to do some setup
>>> from pytz import timezone
>>> utc = timezone('UTC')
>>> eastern = timezone('US/Eastern')
>>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
We next create a datetime right on an end-of-DST transition point,
the instant when the wallclocks are wound back one hour.
>>> 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)'
Now, if we subtract a few minutes from it, note that the timezone
information has not changed.
>>> before = loc_dt - timedelta(minutes=10)
>>> before.strftime(fmt)
'2002-10-27 00:50:00 EST (-0500)'
But we can fix that by calling the normalize method
>>> before = eastern.normalize(before)
>>> before.strftime(fmt)
'2002-10-27 01:50:00 EDT (-0400)'
- tzname(self, dt)
- See datetime.tzinfo.tzname
- utcoffset(self, dt)
- See datetime.tzinfo.utcoffset
Methods inherited from BaseTzInfo:
- __str__(self)
Data descriptors inherited from BaseTzInfo:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Methods inherited from datetime.tzinfo:
- __getattribute__(...)
- x.__getattribute__('name') <==> x.name
- __reduce__(...)
- -> (cls, state)
Data and other attributes inherited from datetime.tzinfo:
- __new__ = <built-in method __new__ of type object at 0x2aaab04fdea0>
- T.__new__(S, ...) -> a new object with type S, a subtype of T
|
class StaticTzInfo(BaseTzInfo) |
| |
A timezone that has a constant offset from UTC
These timezones are rare, as most regions have changed their
offset from UTC at some point in their history |
| |
- Method resolution order:
- StaticTzInfo
- BaseTzInfo
- datetime.tzinfo
- __builtin__.object
Methods defined here:
- __repr__(self)
- dst(self, dt)
- See datetime.tzinfo.dst
- fromutc(self, dt)
- See datetime.tzinfo.fromutc
- localize(self, dt, is_dst=False)
- Convert naive time to local time
- normalize(self, dt, is_dst=False)
- Correct the timezone information on the given datetime
- tzname(self, dt)
- See datetime.tzinfo.tzname
- utcoffset(self, dt)
- See datetime.tzinfo.utcoffset
Methods inherited from BaseTzInfo:
- __str__(self)
Data descriptors inherited from BaseTzInfo:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Methods inherited from datetime.tzinfo:
- __getattribute__(...)
- x.__getattribute__('name') <==> x.name
- __reduce__(...)
- -> (cls, state)
Data and other attributes inherited from datetime.tzinfo:
- __new__ = <built-in method __new__ of type object at 0x2aaab04fdea0>
- T.__new__(S, ...) -> a new object with type S, a subtype of T
| |