diff options
author | Stuart Bishop <stuart@stuartbishop.net> | 2011-05-08 14:48:08 +0700 |
---|---|---|
committer | Stuart Bishop <stuart@stuartbishop.net> | 2011-05-08 14:48:08 +0700 |
commit | 105afaeaad3bff7a409b46e40166699f3a8bf68f (patch) | |
tree | 195e4ad6f1139f5d574557ddade84e36e6df7894 | |
parent | ed8b0b9b86a51703aaf39cf64b3a9b88a92791a7 (diff) | |
download | pytz-git-2011g_release.tar.gz |
Allow StaticTzInfo.normalize() to do timezone conversions like DstTzInfo.normalize() already does. It is still recommended to use datetime.astimezone() though, as it may not be possible to keep supporting normalize() in this fashion2011g_release
-rw-r--r-- | src/pytz/tzinfo.py | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/pytz/tzinfo.py b/src/pytz/tzinfo.py index 2be4fb8..1bb57c2 100644 --- a/src/pytz/tzinfo.py +++ b/src/pytz/tzinfo.py @@ -107,10 +107,33 @@ class StaticTzInfo(BaseTzInfo): return dt.replace(tzinfo=self) def normalize(self, dt, is_dst=False): - '''Correct the timezone information on the given datetime''' + '''Correct the timezone information on the given datetime. + + This is normally a no-op, as StaticTzInfo timezones never have + ambiguous cases to correct: + + >>> from pytz import timezone + >>> gmt = timezone('GMT') + >>> isinstance(gmt, StaticTzInfo) + True + >>> dt = datetime(2011, 5, 8, 1, 2, 3, tzinfo=gmt) + >>> gmt.normalize(dt) is dt + True + + The supported method of converting between timezones is to use + datetime.astimezone(). Currently normalize() also works: + + >>> la = timezone('America/Los_Angeles') + >>> dt = la.localize(datetime(2011, 5, 7, 1, 2, 3)) + >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' + >>> gmt.normalize(dt).strftime(fmt) + '2011-05-07 08:02:03 GMT (+0000)' + ''' + if dt.tzinfo is self: + return dt if dt.tzinfo is None: raise ValueError('Naive time - no tzinfo set') - return dt.replace(tzinfo=self) + return dt.astimezone(self) def __repr__(self): return '<StaticTzInfo %r>' % (self.zone,) @@ -192,6 +215,16 @@ class DstTzInfo(BaseTzInfo): >>> before = eastern.normalize(before) >>> before.strftime(fmt) '2002-10-27 01:50:00 EDT (-0400)' + + The supported method of converting between timezones is to use + datetime.astimezone(). Currently, normalize() also works: + + >>> th = timezone('Asia/Bangkok') + >>> am = timezone('Europe/Amsterdam') + >>> dt = th.localize(datetime(2011, 5, 7, 1, 2, 3)) + >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' + >>> am.normalize(dt).strftime(fmt) + '2011-05-06 20:02:03 CEST (+0200)' ''' if dt.tzinfo is None: raise ValueError('Naive time - no tzinfo set') |