<s> from . constants import MILLI_MICROS , SECOND_MICROS , MINUTE_MICROS <EOL> import calendar <EOL> from datetime import datetime <EOL> from dateutil import parser <EOL> from dateutil . tz import tzlocal <EOL> from . error import TimeConstructionError <EOL> from . sanedelta import SaneDelta <EOL> import pytz <EOL> MICROS_TRANSLATIONS = ( <EOL> ( ( '<STR_LIT:m>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , MINUTE_MICROS ) , <EOL> ( ( '<STR_LIT:s>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , SECOND_MICROS ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , MILLI_MICROS ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:1> ) ) <EOL> MICROS_TRANSLATION_HASH = dict ( ( alt , v ) for k , v in MICROS_TRANSLATIONS for alt in k ) <EOL> class SaneTime ( object ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( time , self ) . __init__ ( ) <EOL> uss = set ( ) <EOL> tzs = set ( ) <EOL> naive_dt = None <EOL> avoid_localize = False <EOL> for k , v in kwargs . iteritems ( ) : <EOL> if k in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> tzs . add ( SaneTime . to_timezone ( v ) ) <EOL> elif k in MICROS_TRANSLATION_HASH : <EOL> uss . add ( MICROS_TRANSLATION_HASH [ k ] * v ) <EOL> else : <EOL> raise TimeConstructionError ( "<STR_LIT>" % ( k , v ) ) <EOL> args = list ( args ) <EOL> if len ( args ) > <NUM_LIT:2> and len ( args ) <= <NUM_LIT:8> : <EOL> args = [ datetime ( * args ) ] <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> tzs . add ( SaneTime . to_timezone ( args . pop ( ) ) ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> arg = args . pop ( ) <EOL> if hasattr ( arg , '<STR_LIT>' ) : <EOL> uss . add ( int ( arg ) ) <EOL> if hasattr ( arg , '<STR_LIT>' ) : tzs . add ( arg . tz ) <EOL> elif isinstance ( arg , basestring ) : <EOL> parts = arg . strip ( ) . split ( '<STR_LIT:U+0020>' ) <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ - <NUM_LIT:1> ] . startswith ( '<STR_LIT:+>' ) : <EOL> try : <EOL> tzs . add ( SaneTime . to_timezone ( parts [ - <NUM_LIT:1> ] [ <NUM_LIT:1> : ] ) ) <EOL> arg = '<STR_LIT:U+0020>' . join ( parts [ : - <NUM_LIT:1> ] ) <EOL> except : pass <EOL> utc = arg . endswith ( '<STR_LIT>' ) or arg . endswith ( '<STR_LIT>' ) <EOL> arg = parser . parse ( arg ) <EOL> if arg . tzinfo : <EOL> if utc : <EOL> tzs . add ( pytz . utc ) <EOL> arg = arg . replace ( tzinfo = None ) <EOL> elif isinstance ( arg . tzinfo , tzlocal ) : <EOL> arg = arg . replace ( tzinfo = None ) <EOL> else : <EOL> avoid_localize = True <EOL> arg = arg . astimezone ( pytz . utc ) . replace ( tzinfo = None ) <EOL> if type ( arg ) == datetime : <EOL> naive_dt = arg <EOL> if naive_dt . tzinfo : <EOL> tzs . add ( SaneTime . to_timezone ( str ( naive_dt . tzinfo ) ) ) <EOL> naive_dt = naive_dt . replace ( tzinfo = None ) <EOL> if len ( tzs ) > <NUM_LIT:1> : <EOL> raise TimeConstructionError ( "<STR_LIT>" % ( tzs ) ) <EOL> self . tz = len ( tzs ) and tzs . pop ( ) or pytz . utc <EOL> if naive_dt : <EOL> if avoid_localize : <EOL> uss . add ( SaneTime . utc_datetime_to_us ( naive_dt ) ) <EOL> else : <EOL> uss . add ( SaneTime . utc_datetime_to_us ( self . tz . localize ( naive_dt ) . astimezone ( pytz . utc ) ) ) <EOL> if len ( uss ) == <NUM_LIT:0> : <EOL> uss . add ( SaneTime . utc_datetime_to_us ( datetime . utcnow ( ) ) ) <EOL> if len ( uss ) > <NUM_LIT:1> : <EOL> raise TimeConstructionError ( "<STR_LIT>" % ( uss ) ) <EOL> self . us = uss . pop ( ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> raise TimeConstructionError ( "<STR_LIT>" ) <EOL> @ property <EOL> def ms ( self ) : return self . us / MILLI_MICROS <EOL> epoch_milliseconds = epoch_millis = milliseconds = millis = ms <EOL> @ property <EOL> def s ( self ) : return self . us / SECOND_MICROS <EOL> epoch_seconds = epoch_secs = seconds = secs = s <EOL> @ property <EOL> def m ( self ) : return self . us / MINUTE_MICROS <EOL> epoch_minutes = epoch_mins = minutes = mins = m <EOL> @ property <EOL> def micros ( self ) : return self . us <EOL> epoch_microseconds = epoch_micros = microseconds = micros <EOL> @ property <EOL> def tz_name ( self ) : return self . tz . zone <EOL> @ property <EOL> def tz_abbr ( self ) : return self . tz . _tzname <EOL> def set_tz ( self , tz ) : <EOL> self . tz = self . __class__ . to_timezone ( tz ) ; return self <EOL> def with_tz ( self , tz ) : <EOL> return self . __class__ ( self . us , tz ) <EOL> @ property <EOL> def _tuple ( self ) : return ( self . us , self . tz ) <EOL> def strftime ( self , * args , ** kwargs ) : return self . datetime . strftime ( * args , ** kwargs ) <EOL> def __cmp__ ( self , other ) : <EOL> if not hasattr ( other , '<STR_LIT>' ) : other = SaneTime ( other ) <EOL> return cmp ( self . us , int ( other ) ) <EOL> def __hash__ ( self ) : return self . us . __hash__ ( ) <EOL> def __add__ ( self , operand ) : <EOL> if not hasattr ( operand , '<STR_LIT>' ) : operand = SaneTime ( operand ) <EOL> return self . __class__ ( self . us + int ( operand ) , tz = self . tz ) <EOL> def __sub__ ( self , operand ) : <EOL> if not hasattr ( operand , '<STR_LIT>' ) : operand = SaneTime ( operand ) <EOL> if isinstance ( operand , SaneTime ) : return SaneDelta ( self . us - int ( operand ) ) <EOL> return self . __add__ ( - int ( operand ) ) <EOL> def __mul__ ( self , operand ) : <EOL> return self . us * int ( operand ) <EOL> def __div__ ( self , operand ) : <EOL> return self . us / int ( operand ) <EOL> def __int__ ( self ) : return int ( self . us ) <EOL> def __long__ ( self ) : return long ( self . us ) <EOL> def __repr__ ( self ) : return u"<STR_LIT>" % ( self . us , repr ( self . tz ) ) <EOL> def __str__ ( self ) : return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> dt = self . datetime <EOL> micros = u"<STR_LIT>" % dt . microsecond if dt . microsecond else '<STR_LIT>' <EOL> time = u"<STR_LIT>" % ( dt . hour , dt . minute , dt . second , micros ) if dt . microsecond or dt . second or dt . minute or dt . hour else '<STR_LIT>' <EOL> return u"<STR_LIT>" % ( dt . year , dt . month , dt . day , time , dt . tzinfo . zone ) <EOL> def clone ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . us , self . tz ) <EOL> @ property <EOL> def ny_str ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . ny_ndt . strftime ( '<STR_LIT>' ) <EOL> @ property <EOL> def utc_datetime ( self ) : return SaneTime . us_to_utc_datetime ( self . us ) <EOL> utc_dt = utc_datetime <EOL> @ property <EOL> def utc_naive_datetime ( self ) : return self . utc_datetime . replace ( tzinfo = None ) <EOL> utc_ndt = utc_naive_datetime <EOL> def to_timezoned_datetime ( self , tz ) : return self . utc_datetime . astimezone ( SaneTime . to_timezone ( tz ) ) <EOL> def to_timezoned_naive_datetime ( self , tz ) : return self . to_timezoned_datetime ( tz ) . replace ( tzinfo = None ) <EOL> @ property <EOL> def datetime ( self ) : return self . to_timezoned_datetime ( self . tz ) <EOL> dt = datetime <EOL> @ property <EOL> def naive_datetime ( self ) : return self . to_timezoned_naive_datetime ( self . tz ) <EOL> ndt = naive_datetime <EOL> @ property <EOL> def ny_datetime ( self ) : return self . to_timezoned_datetime ( '<STR_LIT>' ) <EOL> ny_dt = ny_datetime <EOL> @ property <EOL> def ny_naive_datetime ( self ) : return self . to_timezoned_naive_datetime ( '<STR_LIT>' ) <EOL> ny_ndt = ny_naive_datetime <EOL> @ property <EOL> def year ( self ) : return self . dt . year <EOL> @ property <EOL> def month ( self ) : return self . dt . month <EOL> @ property <EOL> def day ( self ) : return self . dt . day <EOL> @ property <EOL> def hour ( self ) : return self . dt . hour <EOL> @ property <EOL> def minute ( self ) : return self . dt . minute <EOL> @ property <EOL> def second ( self ) : return self . dt . second <EOL> @ property <EOL> def microsecond ( self ) : return self . dt . microsecond <EOL> @ classmethod <EOL> def utc_datetime_to_us ( kls , dt ) : <EOL> return calendar . timegm ( dt . timetuple ( ) ) * <NUM_LIT:1000> ** <NUM_LIT:2> + dt . microsecond <EOL> @ classmethod <EOL> def us_to_utc_datetime ( kls , us ) : <EOL> return pytz . utc . localize ( datetime . utcfromtimestamp ( us / <NUM_LIT:10> ** <NUM_LIT:6> ) ) . replace ( microsecond = us % <NUM_LIT:10> ** <NUM_LIT:6> ) <EOL> @ classmethod <EOL> def to_timezone ( kls , tz ) : <EOL> if not isinstance ( tz , basestring ) : return tz <EOL> return pytz . timezone ( tz ) <EOL> def ntime ( * args , ** kwargs ) : <EOL> if args : <EOL> if args [ <NUM_LIT:0> ] is None : return None <EOL> elif kwargs : <EOL> if None in [ v for k , v in kwargs . iteritems ( ) if k != '<STR_LIT>' ] : return None <EOL> return SaneTime ( * args , ** kwargs ) <EOL> time = sanetime = SaneTime <EOL> nsanetime = ntime </s>
