<s> import re <EOL> import time <EOL> import pytz <EOL> from copy import copy <EOL> from datetime import datetime , timedelta <EOL> from timestring . text2num import text2num <EOL> from timestring import TimestringInvalid <EOL> from timestring . timestring_re import TIMESTRING_RE <EOL> try : <EOL> unicode <EOL> except NameError : <EOL> unicode = str <EOL> long = int <EOL> CLEAN_NUMBER = re . compile ( r"<STR_LIT>" ) <EOL> class Date ( object ) : <EOL> def __init__ ( self , date , offset = None , start_of_week = None , tz = None , verbose = False ) : <EOL> if isinstance ( date , Date ) : <EOL> self . date = copy ( date . date ) <EOL> return <EOL> self . _original = date <EOL> if tz : <EOL> tz = pytz . timezone ( str ( tz ) ) <EOL> if date == '<STR_LIT>' : <EOL> self . date = '<STR_LIT>' <EOL> elif date == '<STR_LIT>' : <EOL> self . date = datetime . now ( ) <EOL> elif type ( date ) in ( str , unicode ) and re . match ( r"<STR_LIT>" , date ) : <EOL> self . date = datetime . strptime ( date [ : - <NUM_LIT:3> ] , "<STR_LIT>" ) - timedelta ( hours = int ( date [ - <NUM_LIT:3> : ] ) ) <EOL> else : <EOL> if type ( date ) in ( str , unicode ) : <EOL> """<STR_LIT>""" <EOL> _date = date . lower ( ) <EOL> res = TIMESTRING_RE . search ( _date . strip ( ) ) <EOL> if res : <EOL> date = res . groupdict ( ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" , '<STR_LIT>' . join ( [ "<STR_LIT>" % ( k , v ) for k , v in date . items ( ) if v ] ) ) <EOL> else : <EOL> raise TimestringInvalid ( '<STR_LIT>' % date ) <EOL> date = dict ( ( k , v if type ( v ) is str else v ) for k , v in date . items ( ) if v ) <EOL> if isinstance ( date , dict ) : <EOL> new_date = datetime ( * time . localtime ( ) [ : <NUM_LIT:3> ] ) <EOL> if tz and tz . zone != "<STR_LIT>" : <EOL> ts = datetime . now ( ) <EOL> dst_start = datetime ( ts . year , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) + timedelta ( <NUM_LIT> - datetime ( ts . year , <NUM_LIT:3> , <NUM_LIT:1> ) . weekday ( ) ) <EOL> dst_end = datetime ( ts . year , <NUM_LIT:11> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:0> ) + timedelta ( <NUM_LIT:6> - datetime ( ts . year , <NUM_LIT:11> , <NUM_LIT:1> ) . weekday ( ) ) <EOL> ts = ts + tz . utcoffset ( new_date , is_dst = ( dst_start < ts < dst_end ) ) <EOL> new_date = datetime ( ts . year , ts . month , ts . day ) <EOL> if date . get ( '<STR_LIT>' ) : <EOL> new_date = datetime . fromtimestamp ( int ( date . get ( '<STR_LIT>' ) ) ) <EOL> elif date . get ( '<STR_LIT>' ) and ( date . get ( '<STR_LIT>' ) or date . get ( '<STR_LIT>' ) ) : <EOL> if date . get ( '<STR_LIT>' , '<STR_LIT>' ) . find ( '<STR_LIT>' ) > - <NUM_LIT:1> : <EOL> i = <NUM_LIT:2> * int ( <NUM_LIT:1> if date . get ( '<STR_LIT>' , True ) or date . get ( '<STR_LIT>' ) == '<STR_LIT>' else - <NUM_LIT:1> ) <EOL> else : <EOL> i = int ( text2num ( date . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) * int ( <NUM_LIT:1> if date . get ( '<STR_LIT>' ) or ( date . get ( '<STR_LIT>' , '<STR_LIT>' ) or '<STR_LIT>' ) == '<STR_LIT>' else - <NUM_LIT:1> ) <EOL> delta = ( date . get ( '<STR_LIT>' ) or date . get ( '<STR_LIT>' ) ) . lower ( ) <EOL> if delta . startswith ( '<STR_LIT:y>' ) : <EOL> try : <EOL> new_date = new_date . replace ( year = ( new_date . year - i ) ) <EOL> except ValueError : <EOL> new_date = new_date - timedelta ( days = ( <NUM_LIT> * i ) ) <EOL> elif delta . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> new_date = new_date . replace ( month = ( new_date . month - i ) ) <EOL> except ValueError : <EOL> new_date = new_date - timedelta ( days = ( <NUM_LIT:30> * i ) ) <EOL> elif delta . startswith ( '<STR_LIT:q>' ) : <EOL> '''<STR_LIT>''' <EOL> q1 , q2 , q3 , q4 = datetime ( new_date . year , <NUM_LIT:1> , <NUM_LIT:1> ) , datetime ( new_date . year , <NUM_LIT:4> , <NUM_LIT:1> ) , datetime ( new_date . year , <NUM_LIT:7> , <NUM_LIT:1> ) , datetime ( new_date . year , <NUM_LIT:10> , <NUM_LIT:1> ) <EOL> if q1 <= new_date < q2 : <EOL> if i == - <NUM_LIT:1> : <EOL> new_date = datetime ( new_date . year - <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT:1> ) <EOL> else : <EOL> new_date = q2 <EOL> elif q2 <= new_date < q3 : <EOL> pass <EOL> elif q3 <= new_date < q4 : <EOL> pass <EOL> else : <EOL> pass <EOL> new_date = new_date - timedelta ( days = ( <NUM_LIT> * i ) ) <EOL> elif delta . startswith ( '<STR_LIT:w>' ) : <EOL> new_date = new_date - timedelta ( days = ( i * <NUM_LIT:7> ) ) <EOL> else : <EOL> new_date = new_date - timedelta ( ** { ( '<STR_LIT>' if delta . startswith ( '<STR_LIT:d>' ) else '<STR_LIT>' if delta . startswith ( '<STR_LIT:h>' ) else '<STR_LIT>' if delta . startswith ( '<STR_LIT:m>' ) else '<STR_LIT>' ) : i } ) <EOL> if [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] : <EOL> dow = max ( [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] ) <EOL> iso = dict ( monday = <NUM_LIT:1> , tuesday = <NUM_LIT:2> , wednesday = <NUM_LIT:3> , thursday = <NUM_LIT:4> , friday = <NUM_LIT:5> , saturday = <NUM_LIT:6> , sunday = <NUM_LIT:7> , mon = <NUM_LIT:1> , tue = <NUM_LIT:2> , tues = <NUM_LIT:2> , wed = <NUM_LIT:3> , wedn = <NUM_LIT:3> , thu = <NUM_LIT:4> , thur = <NUM_LIT:4> , fri = <NUM_LIT:5> , sat = <NUM_LIT:6> , sun = <NUM_LIT:7> ) . get ( dow ) <EOL> if iso : <EOL> if date . get ( '<STR_LIT>' ) not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> days = iso - new_date . isoweekday ( ) - ( <NUM_LIT:7> if iso >= new_date . isoweekday ( ) else <NUM_LIT:0> ) <EOL> else : <EOL> days = iso - new_date . isoweekday ( ) + ( <NUM_LIT:7> if iso < new_date . isoweekday ( ) else <NUM_LIT:0> ) <EOL> new_date = new_date + timedelta ( days = days ) <EOL> elif dow == '<STR_LIT>' : <EOL> new_date = new_date - timedelta ( days = <NUM_LIT:1> ) <EOL> elif dow == '<STR_LIT>' : <EOL> new_date = new_date + timedelta ( days = <NUM_LIT:1> ) <EOL> year = [ int ( CLEAN_NUMBER . sub ( '<STR_LIT>' , date [ key ] ) ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if year : <EOL> year = max ( year ) <EOL> if len ( str ( year ) ) != <NUM_LIT:4> : <EOL> year += <NUM_LIT> if year <= <NUM_LIT> else <NUM_LIT> <EOL> new_date = new_date . replace ( year = year ) <EOL> month = [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if month : <EOL> new_date = new_date . replace ( day = <NUM_LIT:1> ) <EOL> new_date = new_date . replace ( month = int ( max ( month ) ) if re . match ( '<STR_LIT>' , max ( month ) ) else dict ( january = <NUM_LIT:1> , february = <NUM_LIT:2> , march = <NUM_LIT:3> , april = <NUM_LIT:4> , june = <NUM_LIT:6> , july = <NUM_LIT:7> , august = <NUM_LIT:8> , september = <NUM_LIT:9> , october = <NUM_LIT:10> , november = <NUM_LIT:11> , december = <NUM_LIT:12> , jan = <NUM_LIT:1> , feb = <NUM_LIT:2> , mar = <NUM_LIT:3> , apr = <NUM_LIT:4> , may = <NUM_LIT:5> , jun = <NUM_LIT:6> , jul = <NUM_LIT:7> , aug = <NUM_LIT:8> , sep = <NUM_LIT:9> , sept = <NUM_LIT:9> , oct = <NUM_LIT:10> , nov = <NUM_LIT:11> , dec = <NUM_LIT:12> ) . get ( max ( month ) , new_date . month ) ) <EOL> day = [ date . get ( key ) for key in ( '<STR_LIT:date>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if day : <EOL> new_date = new_date . replace ( day = int ( max ( day ) ) ) <EOL> if date . get ( '<STR_LIT>' ) : <EOL> if date [ '<STR_LIT>' ] . find ( '<STR_LIT>' ) >= <NUM_LIT:1> : <EOL> new_date = new_date . replace ( hour = datetime ( * time . localtime ( ) [ : <NUM_LIT:5> ] ) . hour , <EOL> minute = datetime ( * time . localtime ( ) [ : <NUM_LIT:5> ] ) . minute ) <EOL> else : <EOL> new_date = new_date . replace ( hour = dict ( morning = <NUM_LIT:9> , noon = <NUM_LIT:12> , afternoon = <NUM_LIT:15> , evening = <NUM_LIT> , night = <NUM_LIT> , nighttime = <NUM_LIT> , midnight = <NUM_LIT> ) . get ( date . get ( '<STR_LIT>' ) , <NUM_LIT:12> ) ) <EOL> offset = False <EOL> hour = [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if hour : <EOL> new_date = new_date . replace ( hour = int ( max ( hour ) ) ) <EOL> am = [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if am and max ( am ) in ( '<STR_LIT:p>' , '<STR_LIT>' ) : <EOL> h = int ( max ( hour ) ) <EOL> if h < <NUM_LIT:12> : <EOL> new_date = new_date . replace ( hour = h + <NUM_LIT:12> ) <EOL> offset = False <EOL> minute = [ date . get ( key ) for key in ( '<STR_LIT>' , '<STR_LIT>' ) if date . get ( key ) ] <EOL> if minute : <EOL> new_date = new_date . replace ( minute = int ( max ( minute ) ) ) <EOL> seconds = date . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if seconds : <EOL> new_date = new_date . replace ( second = int ( seconds ) ) <EOL> self . date = new_date <EOL> elif type ( date ) in ( int , long , float ) and re . match ( '<STR_LIT>' , str ( date ) ) : <EOL> self . date = datetime . fromtimestamp ( int ( date ) ) <EOL> elif isinstance ( date , datetime ) : <EOL> self . date = date <EOL> elif date is None : <EOL> self . date = datetime . now ( ) <EOL> else : <EOL> self . date = datetime ( * time . localtime ( ) [ : <NUM_LIT:3> ] ) <EOL> if tz : <EOL> self . date = self . date . replace ( tzinfo = tz ) <EOL> if offset and isinstance ( offset , dict ) : <EOL> self . date = self . date . replace ( ** offset ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self ) , id ( self ) ) <EOL> @ property <EOL> def year ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . year <EOL> @ year . setter <EOL> def year ( self , year ) : <EOL> self . date = self . date . replace ( year = year ) <EOL> @ property <EOL> def month ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . month <EOL> @ month . setter <EOL> def month ( self , month ) : <EOL> self . date = self . date . replace ( month = month ) <EOL> @ property <EOL> def day ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . day <EOL> @ day . setter <EOL> def day ( self , day ) : <EOL> self . date = self . date . replace ( day = day ) <EOL> @ property <EOL> def hour ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . hour <EOL> @ hour . setter <EOL> def hour ( self , hour ) : <EOL> self . date = self . date . replace ( hour = hour ) <EOL> @ property <EOL> def minute ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . minute <EOL> @ minute . setter <EOL> def minute ( self , minute ) : <EOL> self . date = self . date . replace ( minute = minute ) <EOL> @ property <EOL> def second ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . second <EOL> @ second . setter <EOL> def second ( self , second ) : <EOL> self . date = self . date . replace ( second = second ) <EOL> @ property <EOL> def weekday ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . isoweekday ( ) <EOL> @ property <EOL> def tz ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . tzinfo <EOL> @ tz . setter <EOL> def tz ( self , tz ) : <EOL> if self . date != '<STR_LIT>' : <EOL> if tz is None : <EOL> self . date = self . date . replace ( tzinfo = None ) <EOL> else : <EOL> self . date = self . date . replace ( tzinfo = pytz . timezone ( tz ) ) <EOL> def replace ( self , ** k ) : <EOL> """<STR_LIT>""" <EOL> if self . date != '<STR_LIT>' : <EOL> return Date ( self . date . replace ( ** k ) ) <EOL> else : <EOL> return Date ( '<STR_LIT>' ) <EOL> def adjust ( self , to ) : <EOL> '''<STR_LIT>''' <EOL> if self . date == '<STR_LIT>' : <EOL> return <EOL> new = copy ( self ) <EOL> if type ( to ) in ( str , unicode ) : <EOL> to = to . lower ( ) <EOL> res = TIMESTRING_RE . search ( to ) <EOL> if res : <EOL> rgroup = res . groupdict ( ) <EOL> if ( rgroup . get ( '<STR_LIT>' ) or rgroup . get ( '<STR_LIT>' ) ) : <EOL> i = int ( text2num ( rgroup . get ( '<STR_LIT>' , '<STR_LIT>' ) ) ) * ( - <NUM_LIT:1> if to . startswith ( '<STR_LIT:->' ) else <NUM_LIT:1> ) <EOL> delta = ( rgroup . get ( '<STR_LIT>' ) or rgroup . get ( '<STR_LIT>' ) ) . lower ( ) <EOL> if delta . startswith ( '<STR_LIT:y>' ) : <EOL> try : <EOL> new . date = new . date . replace ( year = ( new . date . year + i ) ) <EOL> except ValueError : <EOL> new . date = new . date + timedelta ( days = ( <NUM_LIT> * i ) ) <EOL> elif delta . startswith ( '<STR_LIT>' ) : <EOL> if ( new . date . month + i ) > <NUM_LIT:12> : <EOL> new . date = new . date . replace ( month = ( i - ( i / <NUM_LIT:12> ) ) , <EOL> year = ( new . date . year + <NUM_LIT:1> + ( i / <NUM_LIT:12> ) ) ) <EOL> elif ( new . date . month + i ) < <NUM_LIT:1> : <EOL> new . date = new . date . replace ( month = <NUM_LIT:12> , year = ( new . date . year - <NUM_LIT:1> ) ) <EOL> else : <EOL> new . date = new . date . replace ( month = ( new . date . month + i ) ) <EOL> elif delta . startswith ( '<STR_LIT:q>' ) : <EOL> pass <EOL> elif delta . startswith ( '<STR_LIT:w>' ) : <EOL> new . date = new . date + timedelta ( days = ( <NUM_LIT:7> * i ) ) <EOL> elif delta . startswith ( '<STR_LIT:s>' ) : <EOL> new . date = new . date + timedelta ( seconds = i ) <EOL> else : <EOL> new . date = new . date + timedelta ( ** { ( '<STR_LIT>' if delta . startswith ( '<STR_LIT:d>' ) else '<STR_LIT>' if delta . startswith ( '<STR_LIT:h>' ) else '<STR_LIT>' if delta . startswith ( '<STR_LIT:m>' ) else '<STR_LIT>' ) : i } ) <EOL> return new <EOL> else : <EOL> new . date = new . date + timedelta ( seconds = int ( to ) ) <EOL> return new <EOL> raise TimestringInvalid ( '<STR_LIT>' ) <EOL> def __nonzero__ ( self ) : <EOL> return True <EOL> def __add__ ( self , to ) : <EOL> if self . date == '<STR_LIT>' : <EOL> return copy ( self ) <EOL> return copy ( self ) . adjust ( to ) <EOL> def __sub__ ( self , to ) : <EOL> if self . date == '<STR_LIT>' : <EOL> return copy ( self ) <EOL> if type ( to ) in ( str , unicode ) : <EOL> to = to [ <NUM_LIT:1> : ] if to . startswith ( '<STR_LIT:->' ) else ( '<STR_LIT:->' + to ) <EOL> elif type ( to ) in ( int , float , long ) : <EOL> to = to * - <NUM_LIT:1> <EOL> return copy ( self ) . adjust ( to ) <EOL> def __format__ ( self , _ ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . strftime ( '<STR_LIT>' ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . date ) <EOL> def __gt__ ( self , other ) : <EOL> if self . date == '<STR_LIT>' : <EOL> if isinstance ( other , Date ) : <EOL> return other . date != '<STR_LIT>' <EOL> else : <EOL> from . Range import Range <EOL> if isinstance ( other , Range ) : <EOL> return other . end != '<STR_LIT>' <EOL> return other != '<STR_LIT>' <EOL> else : <EOL> if isinstance ( other , Date ) : <EOL> if other . date == '<STR_LIT>' : <EOL> return False <EOL> elif other . tz and self . tz is None : <EOL> return self . date . replace ( tzinfo = other . tz ) > other . date <EOL> elif self . tz and other . tz is None : <EOL> return self . date > other . date . replace ( tzinfo = self . tz ) <EOL> return self . date > other . date <EOL> else : <EOL> from . Range import Range <EOL> if isinstance ( other , Range ) : <EOL> if other . end . date == '<STR_LIT>' : <EOL> return False <EOL> if other . end . tz and self . tz is None : <EOL> return self . date . replace ( tzinfo = other . end . tz ) > other . end . date <EOL> elif self . tz and other . end . tz is None : <EOL> return self . date > other . end . date . replace ( tzinfo = self . tz ) <EOL> return self . date > other . end . date <EOL> else : <EOL> return self . __gt__ ( Date ( other , tz = self . tz ) ) <EOL> def __lt__ ( self , other ) : <EOL> if self . date == '<STR_LIT>' : <EOL> return False <EOL> if isinstance ( other , Date ) : <EOL> if other . date == '<STR_LIT>' : <EOL> return True <EOL> elif other . tz and self . tz is None : <EOL> return self . date . replace ( tzinfo = other . tz ) < other . date <EOL> elif self . tz and other . tz is None : <EOL> return self . date < other . date . replace ( tzinfo = self . tz ) <EOL> return self . date < other . date <EOL> else : <EOL> from . Range import Range <EOL> if isinstance ( other , Range ) : <EOL> if other . end . tz and self . tz is None : <EOL> return self . date . replace ( tzinfo = other . end . tz ) < other . end . date <EOL> elif self . tz and other . end . tz is None : <EOL> return self . date < other . end . date . replace ( tzinfo = self . tz ) <EOL> return self . date < other . end . date <EOL> else : <EOL> return self . __lt__ ( Date ( other , tz = self . tz ) ) <EOL> def __ge__ ( self , other ) : <EOL> return self > other or self == other <EOL> def __le__ ( self , other ) : <EOL> return self < other or self == other <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , datetime ) : <EOL> other = Date ( other ) <EOL> if isinstance ( other , Date ) : <EOL> if other . date == '<STR_LIT>' : <EOL> return self . date == '<STR_LIT>' <EOL> elif other . tz and self . tz is None : <EOL> return self . date . replace ( tzinfo = other . tz ) == other . date <EOL> elif self . tz and other . tz is None : <EOL> return self . date == other . date . replace ( tzinfo = self . tz ) <EOL> return self . date == other . date <EOL> else : <EOL> from . Range import Range <EOL> if isinstance ( other , Range ) : <EOL> return False <EOL> else : <EOL> return self . __eq__ ( Date ( other , tz = self . tz ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def format ( self , format_string = '<STR_LIT>' ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return self . date . strftime ( format_string ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def to_unixtime ( self ) : <EOL> if self . date != '<STR_LIT>' : <EOL> return time . mktime ( self . date . timetuple ( ) ) <EOL> else : <EOL> return - <NUM_LIT:1> </s>
