@@ -1318,19 +1318,42 @@ Using datetime with tzinfo:
13181318
13191319 >>> from datetime import timedelta, datetime, tzinfo, timezone
13201320 >>> class KabulTz (tzinfo ):
1321- ... # Kabul used +4 until 1045, when they moved to +4:30
1321+ ... # Kabul used +4 until 1945, when they moved to +4:30
1322+ ... UTC_MOVE_DATE = datetime(1944 , 12 , 31 , 20 , tzinfo = timezone.utc)
13221323 ... def utcoffset (self , dt ):
1323- ... if dt.year >= 1945 :
1324+ ... if dt.year < 1945 :
1325+ ... return timedelta(hours = 4 )
1326+ ... elif (1945 , 1 , 1 , 0 , 0 ) <= dt.timetuple()[:5 ] < (1945 , 1 , 1 , 0 , 30 ):
1327+ ... # If dt falls in the imaginary range, use fold to decide how
1328+ ... # to resolve. See PEP495
1329+ ... return timedelta(hours = 4 , minutes = (30 if dt.fold else 0 ))
1330+ ... else :
13241331 ... return timedelta(hours = 4 , minutes = 30 )
1332+ ...
1333+ ... def fromutc (self , dt ):
1334+ ... # A custom implementation is required for `fromutc` as
1335+ ... # the input to this function is a datetime with utc values
1336+ ... # but with a tzinfo set to self
1337+ ... # See datetime.astimezone or fromtimestamp
1338+ ...
1339+ ... # Follow same validations as in datetime.tzinfo
1340+ ... if not isinstance (dt, datetime):
1341+ ... raise TypeError (" fromutc() requires a datetime argument" )
1342+ ... if dt.tzinfo is not self :
1343+ ... raise ValueError (" dt.tzinfo is not self" )
1344+ ...
1345+ ... if dt.replace(tzinfo = timezone.utc) >= self .UTC_MOVE_DATE :
1346+ ... return dt + timedelta(hours = 4 , minutes = 30 )
13251347 ... else :
1326- ... return timedelta(hours = 4 )
1348+ ... return dt + timedelta(hours = 4 )
1349+ ...
13271350 ... def dst (self , dt ):
1328- ... return timedelta(0 )
1351+ ... return timedelta(0 )
1352+ ...
13291353 ... def tzname (self ,dt ):
13301354 ... return " Asia/Kabul"
13311355 ...
1332- ...
1333- >>> tz1 = KabulTz()
1356+ >>> tz1 = KabulTz()
13341357 >>> # Datetime before the change
13351358 >>> dt1 = datetime(1900 , 11 , 21 , 16 , 30 , tzinfo = tz1)
13361359 >>> dt1.utcoffset()
@@ -1344,7 +1367,7 @@ Using datetime with tzinfo:
13441367 >>> dt3 # doctest: +ELLIPSIS
13451368 datetime.datetime(2006, 6, 14, 8, 30, tzinfo=datetime.timezone.utc)
13461369 >>> dt2 # doctest: +ELLIPSIS
1347- datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<KabulTz object at 0x...>)
1370+ datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<... KabulTz object at 0x...>)
13481371 >>> dt2.utctimetuple() == dt3.utctimetuple()
13491372 True
13501373
0 commit comments