<s> '''<STR_LIT>''' <EOL> from datetime import datetime , timedelta , tzinfo <EOL> from bisect import bisect_right <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> import pytz <EOL> from pytz . exceptions import AmbiguousTimeError , NonExistentTimeError <EOL> __all__ = [ ] <EOL> _timedelta_cache = { } <EOL> def memorized_timedelta ( seconds ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return _timedelta_cache [ seconds ] <EOL> except KeyError : <EOL> delta = timedelta ( seconds = seconds ) <EOL> _timedelta_cache [ seconds ] = delta <EOL> return delta <EOL> _epoch = datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> _datetime_cache = { <NUM_LIT:0> : _epoch } <EOL> def memorized_datetime ( seconds ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return _datetime_cache [ seconds ] <EOL> except KeyError : <EOL> dt = _epoch + timedelta ( seconds = seconds ) <EOL> _datetime_cache [ seconds ] = dt <EOL> return dt <EOL> _ttinfo_cache = { } <EOL> def memorized_ttinfo ( * args ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return _ttinfo_cache [ args ] <EOL> except KeyError : <EOL> ttinfo = ( <EOL> memorized_timedelta ( args [ <NUM_LIT:0> ] ) , <EOL> memorized_timedelta ( args [ <NUM_LIT:1> ] ) , <EOL> args [ <NUM_LIT:2> ] <EOL> ) <EOL> _ttinfo_cache [ args ] = ttinfo <EOL> return ttinfo <EOL> _notime = memorized_timedelta ( <NUM_LIT:0> ) <EOL> def _to_seconds ( td ) : <EOL> '''<STR_LIT>''' <EOL> return td . seconds + td . days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> class BaseTzInfo ( tzinfo ) : <EOL> _utcoffset = None <EOL> _tzname = None <EOL> zone = None <EOL> def __str__ ( self ) : <EOL> return self . zone <EOL> class StaticTzInfo ( BaseTzInfo ) : <EOL> '''<STR_LIT>''' <EOL> def fromutc ( self , dt ) : <EOL> '''<STR_LIT>''' <EOL> if dt . tzinfo is not None and dt . tzinfo is not self : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return ( dt + self . _utcoffset ) . replace ( tzinfo = self ) <EOL> def utcoffset ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> return self . _utcoffset <EOL> def dst ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> return _notime <EOL> def tzname ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> return self . _tzname <EOL> def localize ( self , dt , is_dst = False ) : <EOL> '''<STR_LIT>''' <EOL> if dt . tzinfo is not None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return dt . replace ( tzinfo = self ) <EOL> def normalize ( self , dt , is_dst = False ) : <EOL> '''<STR_LIT>''' <EOL> if dt . tzinfo is self : <EOL> return dt <EOL> if dt . tzinfo is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return dt . astimezone ( self ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . zone , ) <EOL> def __reduce__ ( self ) : <EOL> return pytz . _p , ( self . zone , ) <EOL> class DstTzInfo ( BaseTzInfo ) : <EOL> '''<STR_LIT>''' <EOL> _utc_transition_times = None <EOL> _transition_info = None <EOL> zone = None <EOL> _tzinfos = None <EOL> _dst = None <EOL> def __init__ ( self , _inf = None , _tzinfos = None ) : <EOL> if _inf : <EOL> self . _tzinfos = _tzinfos <EOL> self . _utcoffset , self . _dst , self . _tzname = _inf <EOL> else : <EOL> _tzinfos = { } <EOL> self . _tzinfos = _tzinfos <EOL> self . _utcoffset , self . _dst , self . _tzname = self . _transition_info [ <NUM_LIT:0> ] <EOL> _tzinfos [ self . _transition_info [ <NUM_LIT:0> ] ] = self <EOL> for inf in self . _transition_info [ <NUM_LIT:1> : ] : <EOL> if inf not in _tzinfos : <EOL> _tzinfos [ inf ] = self . __class__ ( inf , _tzinfos ) <EOL> def fromutc ( self , dt ) : <EOL> '''<STR_LIT>''' <EOL> if ( dt . tzinfo is not None <EOL> and getattr ( dt . tzinfo , '<STR_LIT>' , None ) is not self . _tzinfos ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> dt = dt . replace ( tzinfo = None ) <EOL> idx = max ( <NUM_LIT:0> , bisect_right ( self . _utc_transition_times , dt ) - <NUM_LIT:1> ) <EOL> inf = self . _transition_info [ idx ] <EOL> return ( dt + inf [ <NUM_LIT:0> ] ) . replace ( tzinfo = self . _tzinfos [ inf ] ) <EOL> def normalize ( self , dt ) : <EOL> '''<STR_LIT>''' <EOL> if dt . tzinfo is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> offset = dt . tzinfo . _utcoffset <EOL> dt = dt . replace ( tzinfo = None ) <EOL> dt = dt - offset <EOL> return self . fromutc ( dt ) <EOL> def localize ( self , dt , is_dst = False ) : <EOL> '''<STR_LIT>''' <EOL> if dt . tzinfo is not None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> possible_loc_dt = set ( ) <EOL> for delta in [ timedelta ( days = - <NUM_LIT:1> ) , timedelta ( days = <NUM_LIT:1> ) ] : <EOL> loc_dt = dt + delta <EOL> idx = max ( <NUM_LIT:0> , bisect_right ( <EOL> self . _utc_transition_times , loc_dt ) - <NUM_LIT:1> ) <EOL> inf = self . _transition_info [ idx ] <EOL> tzinfo = self . _tzinfos [ inf ] <EOL> loc_dt = tzinfo . normalize ( dt . replace ( tzinfo = tzinfo ) ) <EOL> if loc_dt . replace ( tzinfo = None ) == dt : <EOL> possible_loc_dt . add ( loc_dt ) <EOL> if len ( possible_loc_dt ) == <NUM_LIT:1> : <EOL> return possible_loc_dt . pop ( ) <EOL> if len ( possible_loc_dt ) == <NUM_LIT:0> : <EOL> if is_dst is None : <EOL> raise NonExistentTimeError ( dt ) <EOL> elif is_dst : <EOL> return self . localize ( <EOL> dt + timedelta ( hours = <NUM_LIT:6> ) , is_dst = True ) - timedelta ( hours = <NUM_LIT:6> ) <EOL> else : <EOL> return self . localize ( <EOL> dt - timedelta ( hours = <NUM_LIT:6> ) , is_dst = False ) + timedelta ( hours = <NUM_LIT:6> ) <EOL> if is_dst is None : <EOL> raise AmbiguousTimeError ( dt ) <EOL> filtered_possible_loc_dt = [ <EOL> p for p in possible_loc_dt <EOL> if bool ( p . tzinfo . _dst ) == is_dst <EOL> ] <EOL> if len ( filtered_possible_loc_dt ) == <NUM_LIT:1> : <EOL> return filtered_possible_loc_dt [ <NUM_LIT:0> ] <EOL> if len ( filtered_possible_loc_dt ) == <NUM_LIT:0> : <EOL> filtered_possible_loc_dt = list ( possible_loc_dt ) <EOL> dates = { } <EOL> for local_dt in filtered_possible_loc_dt : <EOL> utc_time = local_dt . replace ( tzinfo = None ) - local_dt . tzinfo . _utcoffset <EOL> assert utc_time not in dates <EOL> dates [ utc_time ] = local_dt <EOL> return dates [ [ min , max ] [ not is_dst ] ( dates ) ] <EOL> def utcoffset ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> if dt is None : <EOL> return None <EOL> elif dt . tzinfo is not self : <EOL> dt = self . localize ( dt , is_dst ) <EOL> return dt . tzinfo . _utcoffset <EOL> else : <EOL> return self . _utcoffset <EOL> def dst ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> if dt is None : <EOL> return None <EOL> elif dt . tzinfo is not self : <EOL> dt = self . localize ( dt , is_dst ) <EOL> return dt . tzinfo . _dst <EOL> else : <EOL> return self . _dst <EOL> def tzname ( self , dt , is_dst = None ) : <EOL> '''<STR_LIT>''' <EOL> if dt is None : <EOL> return self . zone <EOL> elif dt . tzinfo is not self : <EOL> dt = self . localize ( dt , is_dst ) <EOL> return dt . tzinfo . _tzname <EOL> else : <EOL> return self . _tzname <EOL> def __repr__ ( self ) : <EOL> if self . _dst : <EOL> dst = '<STR_LIT>' <EOL> else : <EOL> dst = '<STR_LIT>' <EOL> if self . _utcoffset > _notime : <EOL> return '<STR_LIT>' % ( <EOL> self . zone , self . _tzname , self . _utcoffset , dst <EOL> ) <EOL> else : <EOL> return '<STR_LIT>' % ( <EOL> self . zone , self . _tzname , self . _utcoffset , dst <EOL> ) <EOL> def __reduce__ ( self ) : <EOL> return pytz . _p , ( <EOL> self . zone , <EOL> _to_seconds ( self . _utcoffset ) , <EOL> _to_seconds ( self . _dst ) , <EOL> self . _tzname <EOL> ) <EOL> def unpickler ( zone , utcoffset = None , dstoffset = None , tzname = None ) : <EOL> """<STR_LIT>""" <EOL> tz = pytz . timezone ( zone ) <EOL> if utcoffset is None : <EOL> return tz <EOL> utcoffset = memorized_timedelta ( utcoffset ) <EOL> dstoffset = memorized_timedelta ( dstoffset ) <EOL> try : <EOL> return tz . _tzinfos [ ( utcoffset , dstoffset , tzname ) ] <EOL> except KeyError : <EOL> pass <EOL> for localized_tz in tz . _tzinfos . values ( ) : <EOL> if ( localized_tz . _utcoffset == utcoffset <EOL> and localized_tz . _dst == dstoffset ) : <EOL> return localized_tz <EOL> inf = ( utcoffset , dstoffset , tzname ) <EOL> tz . _tzinfos [ inf ] = tz . __class__ ( inf , tz . _tzinfos ) <EOL> return tz . _tzinfos [ inf ] </s>
