<s> """<STR_LIT>""" <EOL> import datetime <EOL> import struct <EOL> import time <EOL> import sys <EOL> import os <EOL> import bisect <EOL> import copy <EOL> from contextlib import contextmanager <EOL> from six import string_types , PY3 <EOL> from . _common import tzname_in_python2 , _tzinfo <EOL> try : <EOL> from . win import tzwin , tzwinlocal <EOL> except ImportError : <EOL> tzwin = tzwinlocal = None <EOL> ZERO = datetime . timedelta ( <NUM_LIT:0> ) <EOL> EPOCH = datetime . datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> EPOCHORDINAL = EPOCH . toordinal ( ) <EOL> class tzutc ( datetime . tzinfo ) : <EOL> def utcoffset ( self , dt ) : <EOL> return ZERO <EOL> def dst ( self , dt ) : <EOL> return ZERO <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> return "<STR_LIT>" <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , ( tzutc , tzoffset ) ) : <EOL> return NotImplemented <EOL> return ( isinstance ( other , tzutc ) or <EOL> ( isinstance ( other , tzoffset ) and other . _offset == ZERO ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> __reduce__ = object . __reduce__ <EOL> class tzoffset ( datetime . tzinfo ) : <EOL> def __init__ ( self , name , offset ) : <EOL> self . _name = name <EOL> self . _offset = datetime . timedelta ( seconds = offset ) <EOL> def utcoffset ( self , dt ) : <EOL> return self . _offset <EOL> def dst ( self , dt ) : <EOL> return ZERO <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> return self . _name <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , tzoffset ) : <EOL> return NotImplemented <EOL> return self . _offset == other . _offset <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> repr ( self . _name ) , <EOL> self . _offset . days * <NUM_LIT> + self . _offset . seconds ) <EOL> __reduce__ = object . __reduce__ <EOL> class tzlocal ( _tzinfo ) : <EOL> def __init__ ( self ) : <EOL> super ( tzlocal , self ) . __init__ ( ) <EOL> self . _std_offset = datetime . timedelta ( seconds = - time . timezone ) <EOL> if time . daylight : <EOL> self . _dst_offset = datetime . timedelta ( seconds = - time . altzone ) <EOL> else : <EOL> self . _dst_offset = self . _std_offset <EOL> self . _dst_saved = self . _dst_offset - self . _std_offset <EOL> self . _hasdst = bool ( self . _dst_saved ) <EOL> def utcoffset ( self , dt ) : <EOL> if dt is None and self . _hasdst : <EOL> return None <EOL> if self . _isdst ( dt ) : <EOL> return self . _dst_offset <EOL> else : <EOL> return self . _std_offset <EOL> def dst ( self , dt ) : <EOL> if dt is None and self . _hasdst : <EOL> return None <EOL> if self . _isdst ( dt ) : <EOL> return self . _dst_offset - self . _std_offset <EOL> else : <EOL> return ZERO <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> return time . tzname [ self . _isdst ( dt ) ] <EOL> def _isdst ( self , dt ) : <EOL> if not self . _hasdst : <EOL> return False <EOL> dstval = self . _naive_is_dst ( dt ) <EOL> if not dstval and self . _fold is not None : <EOL> dst_fold_offset = self . _naive_is_dst ( dt - self . _dst_saved ) <EOL> if dst_fold_offset : <EOL> return self . _fold <EOL> return dstval <EOL> def _naive_is_dst ( self , dt ) : <EOL> timestamp = _datetime_to_timestamp ( dt ) <EOL> return time . localtime ( timestamp + time . timezone ) . tm_isdst <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , tzlocal ) : <EOL> return NotImplemented <EOL> return ( self . _std_offset == other . _std_offset and <EOL> self . _dst_offset == other . _dst_offset ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> __reduce__ = object . __reduce__ <EOL> class _ttinfo ( object ) : <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self ) : <EOL> for attr in self . __slots__ : <EOL> setattr ( self , attr , None ) <EOL> def __repr__ ( self ) : <EOL> l = [ ] <EOL> for attr in self . __slots__ : <EOL> value = getattr ( self , attr ) <EOL> if value is not None : <EOL> l . append ( "<STR_LIT>" % ( attr , repr ( value ) ) ) <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( l ) ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , _ttinfo ) : <EOL> return NotImplemented <EOL> return ( self . offset == other . offset and <EOL> self . delta == other . delta and <EOL> self . isdst == other . isdst and <EOL> self . abbr == other . abbr and <EOL> self . isstd == other . isstd and <EOL> self . isgmt == other . isgmt and <EOL> self . dstoffset == other . dstoffset ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __getstate__ ( self ) : <EOL> state = { } <EOL> for name in self . __slots__ : <EOL> state [ name ] = getattr ( self , name , None ) <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> for name in self . __slots__ : <EOL> if name in state : <EOL> setattr ( self , name , state [ name ] ) <EOL> class _tzfile ( object ) : <EOL> """<STR_LIT>""" <EOL> attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> for attr in self . attrs : <EOL> setattr ( self , attr , kwargs . get ( attr , None ) ) <EOL> class tzfile ( _tzinfo ) : <EOL> def __init__ ( self , fileobj , filename = None ) : <EOL> super ( tzfile , self ) . __init__ ( ) <EOL> file_opened_here = False <EOL> if isinstance ( fileobj , string_types ) : <EOL> self . _filename = fileobj <EOL> fileobj = open ( fileobj , '<STR_LIT:rb>' ) <EOL> file_opened_here = True <EOL> elif filename is not None : <EOL> self . _filename = filename <EOL> elif hasattr ( fileobj , "<STR_LIT:name>" ) : <EOL> self . _filename = fileobj . name <EOL> else : <EOL> self . _filename = repr ( fileobj ) <EOL> if fileobj is not None : <EOL> if not file_opened_here : <EOL> fileobj = _ContextWrapper ( fileobj ) <EOL> with fileobj as file_stream : <EOL> tzobj = self . _read_tzfile ( file_stream ) <EOL> self . _set_tzdata ( tzobj ) <EOL> def _set_tzdata ( self , tzobj ) : <EOL> """<STR_LIT>""" <EOL> for attr in _tzfile . attrs : <EOL> setattr ( self , '<STR_LIT:_>' + attr , getattr ( tzobj , attr ) ) <EOL> def _read_tzfile ( self , fileobj ) : <EOL> out = _tzfile ( ) <EOL> if fileobj . read ( <NUM_LIT:4> ) . decode ( ) != "<STR_LIT>" : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> fileobj . read ( <NUM_LIT:16> ) <EOL> ( <EOL> ttisgmtcnt , <EOL> ttisstdcnt , <EOL> leapcnt , <EOL> timecnt , <EOL> typecnt , <EOL> charcnt , <EOL> ) = struct . unpack ( "<STR_LIT>" , fileobj . read ( <NUM_LIT> ) ) <EOL> if timecnt : <EOL> out . trans_list = list ( struct . unpack ( "<STR_LIT>" % timecnt , <EOL> fileobj . read ( timecnt * <NUM_LIT:4> ) ) ) <EOL> else : <EOL> out . trans_list = [ ] <EOL> if timecnt : <EOL> out . trans_idx = struct . unpack ( "<STR_LIT>" % timecnt , <EOL> fileobj . read ( timecnt ) ) <EOL> else : <EOL> out . trans_idx = [ ] <EOL> ttinfo = [ ] <EOL> for i in range ( typecnt ) : <EOL> ttinfo . append ( struct . unpack ( "<STR_LIT>" , fileobj . read ( <NUM_LIT:6> ) ) ) <EOL> abbr = fileobj . read ( charcnt ) . decode ( ) <EOL> if leapcnt : <EOL> leap = struct . unpack ( "<STR_LIT>" % ( leapcnt * <NUM_LIT:2> ) , <EOL> fileobj . read ( leapcnt * <NUM_LIT:8> ) ) <EOL> if ttisstdcnt : <EOL> isstd = struct . unpack ( "<STR_LIT>" % ttisstdcnt , <EOL> fileobj . read ( ttisstdcnt ) ) <EOL> if ttisgmtcnt : <EOL> isgmt = struct . unpack ( "<STR_LIT>" % ttisgmtcnt , <EOL> fileobj . read ( ttisgmtcnt ) ) <EOL> out . ttinfo_list = [ ] <EOL> for i in range ( typecnt ) : <EOL> gmtoff , isdst , abbrind = ttinfo [ i ] <EOL> gmtoff = <NUM_LIT> * ( ( gmtoff + <NUM_LIT:30> ) // <NUM_LIT> ) <EOL> tti = _ttinfo ( ) <EOL> tti . offset = gmtoff <EOL> tti . dstoffset = datetime . timedelta ( <NUM_LIT:0> ) <EOL> tti . delta = datetime . timedelta ( seconds = gmtoff ) <EOL> tti . isdst = isdst <EOL> tti . abbr = abbr [ abbrind : abbr . find ( '<STR_LIT:\x00>' , abbrind ) ] <EOL> tti . isstd = ( ttisstdcnt > i and isstd [ i ] != <NUM_LIT:0> ) <EOL> tti . isgmt = ( ttisgmtcnt > i and isgmt [ i ] != <NUM_LIT:0> ) <EOL> out . ttinfo_list . append ( tti ) <EOL> out . trans_idx = [ out . ttinfo_list [ idx ] for idx in out . trans_idx ] <EOL> out . ttinfo_std = None <EOL> out . ttinfo_dst = None <EOL> out . ttinfo_before = None <EOL> if out . ttinfo_list : <EOL> if not out . trans_list : <EOL> out . ttinfo_std = out . ttinfo_first = out . ttinfo_list [ <NUM_LIT:0> ] <EOL> else : <EOL> for i in range ( timecnt - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> tti = out . trans_idx [ i ] <EOL> if not out . ttinfo_std and not tti . isdst : <EOL> out . ttinfo_std = tti <EOL> elif not out . ttinfo_dst and tti . isdst : <EOL> out . ttinfo_dst = tti <EOL> if out . ttinfo_std and out . ttinfo_dst : <EOL> break <EOL> else : <EOL> if out . ttinfo_dst and not out . ttinfo_std : <EOL> out . ttinfo_std = out . ttinfo_dst <EOL> for tti in out . ttinfo_list : <EOL> if not tti . isdst : <EOL> out . ttinfo_before = tti <EOL> break <EOL> else : <EOL> out . ttinfo_before = out . ttinfo_list [ <NUM_LIT:0> ] <EOL> laststdoffset = None <EOL> for i , tti in enumerate ( out . trans_idx ) : <EOL> if not tti . isdst : <EOL> offset = tti . offset <EOL> laststdoffset = offset <EOL> else : <EOL> if laststdoffset is not None : <EOL> tti . dstoffset = tti . offset - laststdoffset <EOL> out . trans_idx [ i ] = tti <EOL> offset = laststdoffset or <NUM_LIT:0> <EOL> out . trans_list [ i ] += offset <EOL> laststdoffset = None <EOL> for i in reversed ( range ( len ( out . trans_idx ) ) ) : <EOL> tti = out . trans_idx [ i ] <EOL> if tti . isdst : <EOL> if not ( tti . dstoffset or laststdoffset is None ) : <EOL> tti . dstoffset = tti . offset - laststdoffset <EOL> else : <EOL> laststdoffset = tti . offset <EOL> if not isinstance ( tti . dstoffset , datetime . timedelta ) : <EOL> tti . dstoffset = datetime . timedelta ( seconds = tti . dstoffset ) <EOL> out . trans_idx [ i ] = tti <EOL> out . trans_idx = tuple ( out . trans_idx ) <EOL> out . trans_list = tuple ( out . trans_list ) <EOL> return out <EOL> def _find_last_transition ( self , dt ) : <EOL> if not self . _trans_list : <EOL> return None <EOL> timestamp = _datetime_to_timestamp ( dt ) <EOL> idx = bisect . bisect_right ( self . _trans_list , timestamp ) <EOL> return idx - <NUM_LIT:1> <EOL> def _get_ttinfo ( self , idx ) : <EOL> if idx is None or ( idx + <NUM_LIT:1> ) == len ( self . _trans_list ) : <EOL> return self . _ttinfo_std <EOL> if idx < <NUM_LIT:0> : <EOL> return self . _ttinfo_before <EOL> return self . _trans_idx [ idx ] <EOL> def _find_ttinfo ( self , dt ) : <EOL> idx = self . _resolve_ambiguous_time ( dt ) <EOL> return self . _get_ttinfo ( idx ) <EOL> def _resolve_ambiguous_time ( self , dt , idx = None ) : <EOL> if idx is None : <EOL> idx = self . _find_last_transition ( dt ) <EOL> if self . _fold is None or idx is None : <EOL> return idx <EOL> timestamp = _datetime_to_timestamp ( dt ) <EOL> tti = self . _get_ttinfo ( idx ) <EOL> if idx > <NUM_LIT:0> : <EOL> od = self . _get_ttinfo ( idx - <NUM_LIT:1> ) . offset - tti . offset <EOL> tt = self . _trans_list [ idx ] <EOL> if timestamp < tt + od : <EOL> if self . _fold : <EOL> return idx - <NUM_LIT:1> <EOL> else : <EOL> return idx <EOL> if idx < len ( self . _trans_list ) : <EOL> od = self . _get_ttinfo ( idx + <NUM_LIT:1> ) . offset - tti . offset <EOL> tt = self . _trans_list [ idx + <NUM_LIT:1> ] <EOL> if timestamp > tt - od : <EOL> if self . _fold : <EOL> return idx + <NUM_LIT:1> <EOL> else : <EOL> return idx <EOL> return idx <EOL> def utcoffset ( self , dt ) : <EOL> if dt is None : <EOL> return None <EOL> if not self . _ttinfo_std : <EOL> return ZERO <EOL> return self . _find_ttinfo ( dt ) . delta <EOL> def dst ( self , dt ) : <EOL> if not self . _ttinfo_dst : <EOL> return ZERO <EOL> tti = self . _find_ttinfo ( dt ) <EOL> if not tti . isdst : <EOL> return ZERO <EOL> return tti . dstoffset <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> if not self . _ttinfo_std : <EOL> return None <EOL> return self . _find_ttinfo ( dt ) . abbr <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , tzfile ) : <EOL> return NotImplemented <EOL> return ( self . _trans_list == other . _trans_list and <EOL> self . _trans_idx == other . _trans_idx and <EOL> self . _ttinfo_list == other . _ttinfo_list ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , repr ( self . _filename ) ) <EOL> def __reduce__ ( self ) : <EOL> return self . __reduce_ex__ ( None ) <EOL> def __reduce_ex__ ( self , protocol ) : <EOL> return ( self . __class__ , ( None , self . _filename ) , self . __dict__ ) <EOL> class tzrange ( datetime . tzinfo ) : <EOL> def __init__ ( self , stdabbr , stdoffset = None , <EOL> dstabbr = None , dstoffset = None , <EOL> start = None , end = None ) : <EOL> global relativedelta <EOL> from dateutil import relativedelta <EOL> self . _std_abbr = stdabbr <EOL> self . _dst_abbr = dstabbr <EOL> if stdoffset is not None : <EOL> self . _std_offset = datetime . timedelta ( seconds = stdoffset ) <EOL> else : <EOL> self . _std_offset = ZERO <EOL> if dstoffset is not None : <EOL> self . _dst_offset = datetime . timedelta ( seconds = dstoffset ) <EOL> elif dstabbr and stdoffset is not None : <EOL> self . _dst_offset = self . _std_offset + datetime . timedelta ( hours = + <NUM_LIT:1> ) <EOL> else : <EOL> self . _dst_offset = ZERO <EOL> if dstabbr and start is None : <EOL> self . _start_delta = relativedelta . relativedelta ( <EOL> hours = + <NUM_LIT:2> , month = <NUM_LIT:4> , day = <NUM_LIT:1> , weekday = relativedelta . SU ( + <NUM_LIT:1> ) ) <EOL> else : <EOL> self . _start_delta = start <EOL> if dstabbr and end is None : <EOL> self . _end_delta = relativedelta . relativedelta ( <EOL> hours = + <NUM_LIT:1> , month = <NUM_LIT:10> , day = <NUM_LIT> , weekday = relativedelta . SU ( - <NUM_LIT:1> ) ) <EOL> else : <EOL> self . _end_delta = end <EOL> def utcoffset ( self , dt ) : <EOL> if dt is None : <EOL> return None <EOL> if self . _isdst ( dt ) : <EOL> return self . _dst_offset <EOL> else : <EOL> return self . _std_offset <EOL> def dst ( self , dt ) : <EOL> if self . _isdst ( dt ) : <EOL> return self . _dst_offset - self . _std_offset <EOL> else : <EOL> return ZERO <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> if self . _isdst ( dt ) : <EOL> return self . _dst_abbr <EOL> else : <EOL> return self . _std_abbr <EOL> def _isdst ( self , dt ) : <EOL> if not self . _start_delta : <EOL> return False <EOL> year = datetime . datetime ( dt . year , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> start = year + self . _start_delta <EOL> end = year + self . _end_delta <EOL> dt = dt . replace ( tzinfo = None ) <EOL> if start < end : <EOL> return dt >= start and dt < end <EOL> else : <EOL> return dt >= start or dt < end <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , tzrange ) : <EOL> return NotImplemented <EOL> return ( self . _std_abbr == other . _std_abbr and <EOL> self . _dst_abbr == other . _dst_abbr and <EOL> self . _std_offset == other . _std_offset and <EOL> self . _dst_offset == other . _dst_offset and <EOL> self . _start_delta == other . _start_delta and <EOL> self . _end_delta == other . _end_delta ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . __class__ . __name__ <EOL> __reduce__ = object . __reduce__ <EOL> class tzstr ( tzrange ) : <EOL> def __init__ ( self , s ) : <EOL> global parser <EOL> from dateutil import parser <EOL> self . _s = s <EOL> res = parser . _parsetz ( s ) <EOL> if res is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if res . stdabbr in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> res . stdoffset *= - <NUM_LIT:1> <EOL> tzrange . __init__ ( self , res . stdabbr , res . stdoffset , <EOL> res . dstabbr , res . dstoffset , <EOL> start = False , end = False ) <EOL> if not res . dstabbr : <EOL> self . _start_delta = None <EOL> self . _end_delta = None <EOL> else : <EOL> self . _start_delta = self . _delta ( res . start ) <EOL> if self . _start_delta : <EOL> self . _end_delta = self . _delta ( res . end , isend = <NUM_LIT:1> ) <EOL> def _delta ( self , x , isend = <NUM_LIT:0> ) : <EOL> kwargs = { } <EOL> if x . month is not None : <EOL> kwargs [ "<STR_LIT>" ] = x . month <EOL> if x . weekday is not None : <EOL> kwargs [ "<STR_LIT>" ] = relativedelta . weekday ( x . weekday , x . week ) <EOL> if x . week > <NUM_LIT:0> : <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT:1> <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT> <EOL> elif x . day : <EOL> kwargs [ "<STR_LIT>" ] = x . day <EOL> elif x . yday is not None : <EOL> kwargs [ "<STR_LIT>" ] = x . yday <EOL> elif x . jyday is not None : <EOL> kwargs [ "<STR_LIT>" ] = x . jyday <EOL> if not kwargs : <EOL> if not isend : <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT:4> <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT:1> <EOL> kwargs [ "<STR_LIT>" ] = relativedelta . SU ( + <NUM_LIT:1> ) <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT:10> <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT> <EOL> kwargs [ "<STR_LIT>" ] = relativedelta . SU ( - <NUM_LIT:1> ) <EOL> if x . time is not None : <EOL> kwargs [ "<STR_LIT>" ] = x . time <EOL> else : <EOL> kwargs [ "<STR_LIT>" ] = <NUM_LIT> <EOL> if isend : <EOL> delta = self . _dst_offset - self . _std_offset <EOL> kwargs [ "<STR_LIT>" ] -= delta . seconds + delta . days * <NUM_LIT> <EOL> return relativedelta . relativedelta ( ** kwargs ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , repr ( self . _s ) ) <EOL> class _tzicalvtzcomp ( object ) : <EOL> def __init__ ( self , tzoffsetfrom , tzoffsetto , isdst , <EOL> tzname = None , rrule = None ) : <EOL> self . tzoffsetfrom = datetime . timedelta ( seconds = tzoffsetfrom ) <EOL> self . tzoffsetto = datetime . timedelta ( seconds = tzoffsetto ) <EOL> self . tzoffsetdiff = self . tzoffsetto - self . tzoffsetfrom <EOL> self . isdst = isdst <EOL> self . tzname = tzname <EOL> self . rrule = rrule <EOL> class _tzicalvtz ( datetime . tzinfo ) : <EOL> def __init__ ( self , tzid , comps = [ ] ) : <EOL> self . _tzid = tzid <EOL> self . _comps = comps <EOL> self . _cachedate = [ ] <EOL> self . _cachecomp = [ ] <EOL> def _find_comp ( self , dt ) : <EOL> if len ( self . _comps ) == <NUM_LIT:1> : <EOL> return self . _comps [ <NUM_LIT:0> ] <EOL> dt = dt . replace ( tzinfo = None ) <EOL> try : <EOL> return self . _cachecomp [ self . _cachedate . index ( dt ) ] <EOL> except ValueError : <EOL> pass <EOL> lastcomp = None <EOL> lastcompdt = None <EOL> for comp in self . _comps : <EOL> if not comp . isdst : <EOL> compdt = comp . rrule . before ( dt - comp . tzoffsetdiff , inc = True ) <EOL> else : <EOL> compdt = comp . rrule . before ( dt , inc = True ) <EOL> if compdt and ( not lastcompdt or lastcompdt < compdt ) : <EOL> lastcompdt = compdt <EOL> lastcomp = comp <EOL> if not lastcomp : <EOL> for comp in self . _comps : <EOL> if not comp . isdst : <EOL> lastcomp = comp <EOL> break <EOL> else : <EOL> lastcomp = comp [ <NUM_LIT:0> ] <EOL> self . _cachedate . insert ( <NUM_LIT:0> , dt ) <EOL> self . _cachecomp . insert ( <NUM_LIT:0> , lastcomp ) <EOL> if len ( self . _cachedate ) > <NUM_LIT:10> : <EOL> self . _cachedate . pop ( ) <EOL> self . _cachecomp . pop ( ) <EOL> return lastcomp <EOL> def utcoffset ( self , dt ) : <EOL> if dt is None : <EOL> return None <EOL> return self . _find_comp ( dt ) . tzoffsetto <EOL> def dst ( self , dt ) : <EOL> comp = self . _find_comp ( dt ) <EOL> if comp . isdst : <EOL> return comp . tzoffsetdiff <EOL> else : <EOL> return ZERO <EOL> @ tzname_in_python2 <EOL> def tzname ( self , dt ) : <EOL> return self . _find_comp ( dt ) . tzname <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % repr ( self . _tzid ) <EOL> __reduce__ = object . __reduce__ <EOL> class tzical ( object ) : <EOL> def __init__ ( self , fileobj ) : <EOL> global rrule <EOL> from dateutil import rrule <EOL> if isinstance ( fileobj , string_types ) : <EOL> self . _s = fileobj <EOL> fileobj = open ( fileobj , '<STR_LIT:r>' ) <EOL> elif hasattr ( fileobj , "<STR_LIT:name>" ) : <EOL> self . _s = fileobj . name <EOL> else : <EOL> self . _s = repr ( fileobj ) <EOL> self . _vtz = { } <EOL> self . _parse_rfc ( fileobj . read ( ) ) <EOL> def keys ( self ) : <EOL> return list ( self . _vtz . keys ( ) ) <EOL> def get ( self , tzid = None ) : <EOL> if tzid is None : <EOL> keys = list ( self . _vtz . keys ( ) ) <EOL> if len ( keys ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif len ( keys ) > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> tzid = keys [ <NUM_LIT:0> ] <EOL> return self . _vtz . get ( tzid ) <EOL> def _parse_offset ( self , s ) : <EOL> s = s . strip ( ) <EOL> if not s : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if s [ <NUM_LIT:0> ] in ( '<STR_LIT:+>' , '<STR_LIT:->' ) : <EOL> signal = ( - <NUM_LIT:1> , + <NUM_LIT:1> ) [ s [ <NUM_LIT:0> ] == '<STR_LIT:+>' ] <EOL> s = s [ <NUM_LIT:1> : ] <EOL> else : <EOL> signal = + <NUM_LIT:1> <EOL> if len ( s ) == <NUM_LIT:4> : <EOL> return ( int ( s [ : <NUM_LIT:2> ] ) * <NUM_LIT> + int ( s [ <NUM_LIT:2> : ] ) * <NUM_LIT> ) * signal <EOL> elif len ( s ) == <NUM_LIT:6> : <EOL> return ( int ( s [ : <NUM_LIT:2> ] ) * <NUM_LIT> + int ( s [ <NUM_LIT:2> : <NUM_LIT:4> ] ) * <NUM_LIT> + int ( s [ <NUM_LIT:4> : ] ) ) * signal <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + s ) <EOL> def _parse_rfc ( self , s ) : <EOL> lines = s . splitlines ( ) <EOL> if not lines : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> i = <NUM_LIT:0> <EOL> while i < len ( lines ) : <EOL> line = lines [ i ] . rstrip ( ) <EOL> if not line : <EOL> del lines [ i ] <EOL> elif i > <NUM_LIT:0> and line [ <NUM_LIT:0> ] == "<STR_LIT:U+0020>" : <EOL> lines [ i - <NUM_LIT:1> ] += line [ <NUM_LIT:1> : ] <EOL> del lines [ i ] <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> tzid = None <EOL> comps = [ ] <EOL> invtz = False <EOL> comptype = None <EOL> for line in lines : <EOL> if not line : <EOL> continue <EOL> name , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> parms = name . split ( '<STR_LIT:;>' ) <EOL> if not parms : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> name = parms [ <NUM_LIT:0> ] . upper ( ) <EOL> parms = parms [ <NUM_LIT:1> : ] <EOL> if invtz : <EOL> if name == "<STR_LIT>" : <EOL> if value in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> pass <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + value ) <EOL> comptype = value <EOL> founddtstart = False <EOL> tzoffsetfrom = None <EOL> tzoffsetto = None <EOL> rrulelines = [ ] <EOL> tzname = None <EOL> elif name == "<STR_LIT>" : <EOL> if value == "<STR_LIT>" : <EOL> if comptype : <EOL> raise ValueError ( "<STR_LIT>" + comptype ) <EOL> if not tzid : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not comps : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> self . _vtz [ tzid ] = _tzicalvtz ( tzid , comps ) <EOL> invtz = False <EOL> elif value == comptype : <EOL> if not founddtstart : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if tzoffsetfrom is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if tzoffsetto is None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> rr = None <EOL> if rrulelines : <EOL> rr = rrule . rrulestr ( "<STR_LIT:\n>" . join ( rrulelines ) , <EOL> compatible = True , <EOL> ignoretz = True , <EOL> cache = True ) <EOL> comp = _tzicalvtzcomp ( tzoffsetfrom , tzoffsetto , <EOL> ( comptype == "<STR_LIT>" ) , <EOL> tzname , rr ) <EOL> comps . append ( comp ) <EOL> comptype = None <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + value ) <EOL> elif comptype : <EOL> if name == "<STR_LIT>" : <EOL> rrulelines . append ( line ) <EOL> founddtstart = True <EOL> elif name in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> rrulelines . append ( line ) <EOL> elif name == "<STR_LIT>" : <EOL> if parms : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( name , parms [ <NUM_LIT:0> ] ) ) <EOL> tzoffsetfrom = self . _parse_offset ( value ) <EOL> elif name == "<STR_LIT>" : <EOL> if parms : <EOL> raise ValueError ( <EOL> "<STR_LIT>" + parms [ <NUM_LIT:0> ] ) <EOL> tzoffsetto = self . _parse_offset ( value ) <EOL> elif name == "<STR_LIT>" : <EOL> if parms : <EOL> raise ValueError ( <EOL> "<STR_LIT>" + parms [ <NUM_LIT:0> ] ) <EOL> tzname = value <EOL> elif name == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + name ) <EOL> else : <EOL> if name == "<STR_LIT>" : <EOL> if parms : <EOL> raise ValueError ( <EOL> "<STR_LIT>" + parms [ <NUM_LIT:0> ] ) <EOL> tzid = value <EOL> elif name in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> pass <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + name ) <EOL> elif name == "<STR_LIT>" and value == "<STR_LIT>" : <EOL> tzid = None <EOL> comps = [ ] <EOL> invtz = True <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , repr ( self . _s ) ) <EOL> if sys . platform != "<STR_LIT:win32>" : <EOL> TZFILES = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> TZPATHS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> TZFILES = [ ] <EOL> TZPATHS = [ ] <EOL> def gettz ( name = None ) : <EOL> tz = None <EOL> if not name : <EOL> try : <EOL> name = os . environ [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> if name is None or name == "<STR_LIT::>" : <EOL> for filepath in TZFILES : <EOL> if not os . path . isabs ( filepath ) : <EOL> filename = filepath <EOL> for path in TZPATHS : <EOL> filepath = os . path . join ( path , filename ) <EOL> if os . path . isfile ( filepath ) : <EOL> break <EOL> else : <EOL> continue <EOL> if os . path . isfile ( filepath ) : <EOL> try : <EOL> tz = tzfile ( filepath ) <EOL> break <EOL> except ( IOError , OSError , ValueError ) : <EOL> pass <EOL> else : <EOL> tz = tzlocal ( ) <EOL> else : <EOL> if name . startswith ( "<STR_LIT::>" ) : <EOL> name = name [ : - <NUM_LIT:1> ] <EOL> if os . path . isabs ( name ) : <EOL> if os . path . isfile ( name ) : <EOL> tz = tzfile ( name ) <EOL> else : <EOL> tz = None <EOL> else : <EOL> for path in TZPATHS : <EOL> filepath = os . path . join ( path , name ) <EOL> if not os . path . isfile ( filepath ) : <EOL> filepath = filepath . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> if not os . path . isfile ( filepath ) : <EOL> continue <EOL> try : <EOL> tz = tzfile ( filepath ) <EOL> break <EOL> except ( IOError , OSError , ValueError ) : <EOL> pass <EOL> else : <EOL> tz = None <EOL> if tzwin is not None : <EOL> try : <EOL> tz = tzwin ( name ) <EOL> except WindowsError : <EOL> tz = None <EOL> if not tz : <EOL> from dateutil . zoneinfo import gettz <EOL> tz = gettz ( name ) <EOL> if not tz : <EOL> for c in name : <EOL> if c in "<STR_LIT>" : <EOL> try : <EOL> tz = tzstr ( name ) <EOL> except ValueError : <EOL> pass <EOL> break <EOL> else : <EOL> if name in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> tz = tzutc ( ) <EOL> elif name in time . tzname : <EOL> tz = tzlocal ( ) <EOL> return tz <EOL> def _total_seconds ( td ) : <EOL> return ( ( td . seconds + td . days * <NUM_LIT> ) * <NUM_LIT> + <EOL> td . microseconds ) // <NUM_LIT> <EOL> _total_seconds = getattr ( datetime . timedelta , '<STR_LIT>' , _total_seconds ) <EOL> def _datetime_to_timestamp ( dt ) : <EOL> """<STR_LIT>""" <EOL> return _total_seconds ( ( dt . replace ( tzinfo = None ) - EPOCH ) ) <EOL> class _ContextWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context ) : <EOL> self . context = context <EOL> def __enter__ ( self ) : <EOL> return self . context <EOL> def __exit__ ( * args , ** kwargs ) : <EOL> pass </s>
