@@ -107,10 +107,33 @@ def localize(self, dt, is_dst=False):
107107 return dt .replace (tzinfo = self )
108108
109109 def normalize (self , dt , is_dst = False ):
110- '''Correct the timezone information on the given datetime'''
110+ '''Correct the timezone information on the given datetime.
111+
112+ This is normally a no-op, as StaticTzInfo timezones never have
113+ ambiguous cases to correct:
114+
115+ >>> from pytz import timezone
116+ >>> gmt = timezone('GMT')
117+ >>> isinstance(gmt, StaticTzInfo)
118+ True
119+ >>> dt = datetime(2011, 5, 8, 1, 2, 3, tzinfo=gmt)
120+ >>> gmt.normalize(dt) is dt
121+ True
122+
123+ The supported method of converting between timezones is to use
124+ datetime.astimezone(). Currently normalize() also works:
125+
126+ >>> la = timezone('America/Los_Angeles')
127+ >>> dt = la.localize(datetime(2011, 5, 7, 1, 2, 3))
128+ >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
129+ >>> gmt.normalize(dt).strftime(fmt)
130+ '2011-05-07 08:02:03 GMT (+0000)'
131+ '''
132+ if dt .tzinfo is self :
133+ return dt
111134 if dt .tzinfo is None :
112135 raise ValueError ('Naive time - no tzinfo set' )
113- return dt .replace ( tzinfo = self )
136+ return dt .astimezone ( self )
114137
115138 def __repr__ (self ):
116139 return '<StaticTzInfo %r>' % (self .zone ,)
@@ -192,6 +215,16 @@ def normalize(self, dt):
192215 >>> before = eastern.normalize(before)
193216 >>> before.strftime(fmt)
194217 '2002-10-27 01:50:00 EDT (-0400)'
218+
219+ The supported method of converting between timezones is to use
220+ datetime.astimezone(). Currently, normalize() also works:
221+
222+ >>> th = timezone('Asia/Bangkok')
223+ >>> am = timezone('Europe/Amsterdam')
224+ >>> dt = th.localize(datetime(2011, 5, 7, 1, 2, 3))
225+ >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)'
226+ >>> am.normalize(dt).strftime(fmt)
227+ '2011-05-06 20:02:03 CEST (+0200)'
195228 '''
196229 if dt .tzinfo is None :
197230 raise ValueError ('Naive time - no tzinfo set' )
0 commit comments