<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> import datetime <EOL> import calendar <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class weekday ( object ) : <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT:n>" ] <EOL> def __init__ ( self , weekday , n = None ) : <EOL> self . weekday = weekday <EOL> self . n = n <EOL> def __call__ ( self , n ) : <EOL> if n == self . n : <EOL> return self <EOL> else : <EOL> return self . __class__ ( self . weekday , n ) <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> if self . weekday != other . weekday or self . n != other . n : <EOL> return False <EOL> except AttributeError : <EOL> return False <EOL> return True <EOL> def __repr__ ( self ) : <EOL> s = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) [ self . weekday ] <EOL> if not self . n : <EOL> return s <EOL> else : <EOL> return "<STR_LIT>" % ( s , self . n ) <EOL> MO , TU , WE , TH , FR , SA , SU = weekdays = tuple ( [ weekday ( x ) for x in range ( <NUM_LIT:7> ) ] ) <EOL> class relativedelta : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dt1 = None , dt2 = None , <EOL> years = <NUM_LIT:0> , months = <NUM_LIT:0> , days = <NUM_LIT:0> , leapdays = <NUM_LIT:0> , weeks = <NUM_LIT:0> , <EOL> hours = <NUM_LIT:0> , minutes = <NUM_LIT:0> , seconds = <NUM_LIT:0> , microseconds = <NUM_LIT:0> , <EOL> year = None , month = None , day = None , weekday = None , <EOL> yearday = None , nlyearday = None , <EOL> hour = None , minute = None , second = None , microsecond = None ) : <EOL> if dt1 and dt2 : <EOL> if not isinstance ( dt1 , datetime . date ) or not isinstance ( dt2 , datetime . date ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> if type ( dt1 ) is not type ( dt2 ) : <EOL> if not isinstance ( dt1 , datetime . datetime ) : <EOL> dt1 = datetime . datetime . fromordinal ( dt1 . toordinal ( ) ) <EOL> elif not isinstance ( dt2 , datetime . datetime ) : <EOL> dt2 = datetime . datetime . fromordinal ( dt2 . toordinal ( ) ) <EOL> self . years = <NUM_LIT:0> <EOL> self . months = <NUM_LIT:0> <EOL> self . days = <NUM_LIT:0> <EOL> self . leapdays = <NUM_LIT:0> <EOL> self . hours = <NUM_LIT:0> <EOL> self . minutes = <NUM_LIT:0> <EOL> self . seconds = <NUM_LIT:0> <EOL> self . microseconds = <NUM_LIT:0> <EOL> self . year = None <EOL> self . month = None <EOL> self . day = None <EOL> self . weekday = None <EOL> self . hour = None <EOL> self . minute = None <EOL> self . second = None <EOL> self . microsecond = None <EOL> self . _has_time = <NUM_LIT:0> <EOL> months = ( dt1 . year * <NUM_LIT:12> + dt1 . month ) - ( dt2 . year * <NUM_LIT:12> + dt2 . month ) <EOL> self . _set_months ( months ) <EOL> dtm = self . __radd__ ( dt2 ) <EOL> if dt1 < dt2 : <EOL> while dt1 > dtm : <EOL> months += <NUM_LIT:1> <EOL> self . _set_months ( months ) <EOL> dtm = self . __radd__ ( dt2 ) <EOL> else : <EOL> while dt1 < dtm : <EOL> months -= <NUM_LIT:1> <EOL> self . _set_months ( months ) <EOL> dtm = self . __radd__ ( dt2 ) <EOL> delta = dt1 - dtm <EOL> self . seconds = delta . seconds + delta . days * <NUM_LIT> <EOL> self . microseconds = delta . microseconds <EOL> else : <EOL> self . years = years <EOL> self . months = months <EOL> self . days = days + weeks * <NUM_LIT:7> <EOL> self . leapdays = leapdays <EOL> self . hours = hours <EOL> self . minutes = minutes <EOL> self . seconds = seconds <EOL> self . microseconds = microseconds <EOL> self . year = year <EOL> self . month = month <EOL> self . day = day <EOL> self . hour = hour <EOL> self . minute = minute <EOL> self . second = second <EOL> self . microsecond = microsecond <EOL> if type ( weekday ) is int : <EOL> self . weekday = weekdays [ weekday ] <EOL> else : <EOL> self . weekday = weekday <EOL> yday = <NUM_LIT:0> <EOL> if nlyearday : <EOL> yday = nlyearday <EOL> elif yearday : <EOL> yday = yearday <EOL> if yearday > <NUM_LIT> : <EOL> self . leapdays = - <NUM_LIT:1> <EOL> if yday : <EOL> ydayidx = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> for idx , ydays in enumerate ( ydayidx ) : <EOL> if yday <= ydays : <EOL> self . month = idx + <NUM_LIT:1> <EOL> if idx == <NUM_LIT:0> : <EOL> self . day = ydays <EOL> else : <EOL> self . day = yday - ydayidx [ idx - <NUM_LIT:1> ] <EOL> break <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % yday <EOL> self . _fix ( ) <EOL> def _fix ( self ) : <EOL> if abs ( self . microseconds ) > <NUM_LIT> : <EOL> s = self . microseconds / abs ( self . microseconds ) <EOL> div , mod = divmod ( self . microseconds * s , <NUM_LIT> ) <EOL> self . microseconds = mod * s <EOL> self . seconds += div * s <EOL> if abs ( self . seconds ) > <NUM_LIT> : <EOL> s = self . seconds / abs ( self . seconds ) <EOL> div , mod = divmod ( self . seconds * s , <NUM_LIT> ) <EOL> self . seconds = mod * s <EOL> self . minutes += div * s <EOL> if abs ( self . minutes ) > <NUM_LIT> : <EOL> s = self . minutes / abs ( self . minutes ) <EOL> div , mod = divmod ( self . minutes * s , <NUM_LIT> ) <EOL> self . minutes = mod * s <EOL> self . hours += div * s <EOL> if abs ( self . hours ) > <NUM_LIT> : <EOL> s = self . hours / abs ( self . hours ) <EOL> div , mod = divmod ( self . hours * s , <NUM_LIT> ) <EOL> self . hours = mod * s <EOL> self . days += div * s <EOL> if abs ( self . months ) > <NUM_LIT:11> : <EOL> s = self . months / abs ( self . months ) <EOL> div , mod = divmod ( self . months * s , <NUM_LIT:12> ) <EOL> self . months = mod * s <EOL> self . years += div * s <EOL> if ( self . hours or self . minutes or self . seconds or self . microseconds or <EOL> self . hour is not None or self . minute is not None or <EOL> self . second is not None or self . microsecond is not None ) : <EOL> self . _has_time = <NUM_LIT:1> <EOL> else : <EOL> self . _has_time = <NUM_LIT:0> <EOL> def _set_months ( self , months ) : <EOL> self . months = months <EOL> if abs ( self . months ) > <NUM_LIT:11> : <EOL> s = self . months / abs ( self . months ) <EOL> div , mod = divmod ( self . months * s , <NUM_LIT:12> ) <EOL> self . months = mod * s <EOL> self . years = div * s <EOL> else : <EOL> self . years = <NUM_LIT:0> <EOL> def __radd__ ( self , other ) : <EOL> if not isinstance ( other , datetime . date ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> elif self . _has_time and not isinstance ( other , datetime . datetime ) : <EOL> other = datetime . datetime . fromordinal ( other . toordinal ( ) ) <EOL> year = ( self . year or other . year ) + self . years <EOL> month = self . month or other . month <EOL> if self . months : <EOL> assert <NUM_LIT:1> <= abs ( self . months ) <= <NUM_LIT:12> <EOL> month += self . months <EOL> if month > <NUM_LIT:12> : <EOL> year += <NUM_LIT:1> <EOL> month -= <NUM_LIT:12> <EOL> elif month < <NUM_LIT:1> : <EOL> year -= <NUM_LIT:1> <EOL> month += <NUM_LIT:12> <EOL> day = min ( calendar . monthrange ( year , month ) [ <NUM_LIT:1> ] , <EOL> self . day or other . day ) <EOL> repl = { "<STR_LIT>" : year , "<STR_LIT>" : month , "<STR_LIT>" : day } <EOL> for attr in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> value = getattr ( self , attr ) <EOL> if value is not None : <EOL> repl [ attr ] = value <EOL> days = self . days <EOL> if self . leapdays and month > <NUM_LIT:2> and calendar . isleap ( year ) : <EOL> days += self . leapdays <EOL> ret = ( other . replace ( ** repl ) <EOL> + datetime . timedelta ( days = days , <EOL> hours = self . hours , <EOL> minutes = self . minutes , <EOL> seconds = self . seconds , <EOL> microseconds = self . microseconds ) ) <EOL> if self . weekday : <EOL> weekday , nth = self . weekday . weekday , self . weekday . n or <NUM_LIT:1> <EOL> jumpdays = ( abs ( nth ) - <NUM_LIT:1> ) * <NUM_LIT:7> <EOL> if nth > <NUM_LIT:0> : <EOL> jumpdays += ( <NUM_LIT:7> - ret . weekday ( ) + weekday ) % <NUM_LIT:7> <EOL> else : <EOL> jumpdays += ( ret . weekday ( ) - weekday ) % <NUM_LIT:7> <EOL> jumpdays *= - <NUM_LIT:1> <EOL> ret += datetime . timedelta ( days = jumpdays ) <EOL> return ret <EOL> def __rsub__ ( self , other ) : <EOL> return self . __neg__ ( ) . __radd__ ( other ) <EOL> def __add__ ( self , other ) : <EOL> if not isinstance ( other , relativedelta ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> return relativedelta ( years = other . years + self . years , <EOL> months = other . months + self . months , <EOL> days = other . days + self . days , <EOL> hours = other . hours + self . hours , <EOL> minutes = other . minutes + self . minutes , <EOL> seconds = other . seconds + self . seconds , <EOL> microseconds = other . microseconds + self . microseconds , <EOL> leapdays = other . leapdays or self . leapdays , <EOL> year = other . year or self . year , <EOL> month = other . month or self . month , <EOL> day = other . day or self . day , <EOL> weekday = other . weekday or self . weekday , <EOL> hour = other . hour or self . hour , <EOL> minute = other . minute or self . minute , <EOL> second = other . second or self . second , <EOL> microsecond = other . second or self . microsecond ) <EOL> def __sub__ ( self , other ) : <EOL> if not isinstance ( other , relativedelta ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> return relativedelta ( years = other . years - self . years , <EOL> months = other . months - self . months , <EOL> days = other . days - self . days , <EOL> hours = other . hours - self . hours , <EOL> minutes = other . minutes - self . minutes , <EOL> seconds = other . seconds - self . seconds , <EOL> microseconds = other . microseconds - self . microseconds , <EOL> leapdays = other . leapdays or self . leapdays , <EOL> year = other . year or self . year , <EOL> month = other . month or self . month , <EOL> day = other . day or self . day , <EOL> weekday = other . weekday or self . weekday , <EOL> hour = other . hour or self . hour , <EOL> minute = other . minute or self . minute , <EOL> second = other . second or self . second , <EOL> microsecond = other . second or self . microsecond ) <EOL> def __neg__ ( self ) : <EOL> return relativedelta ( years = - self . years , <EOL> months = - self . months , <EOL> days = - self . days , <EOL> hours = - self . hours , <EOL> minutes = - self . minutes , <EOL> seconds = - self . seconds , <EOL> microseconds = - self . microseconds , <EOL> leapdays = self . leapdays , <EOL> year = self . year , <EOL> month = self . month , <EOL> day = self . day , <EOL> weekday = self . weekday , <EOL> hour = self . hour , <EOL> minute = self . minute , <EOL> second = self . second , <EOL> microsecond = self . microsecond ) <EOL> def __nonzero__ ( self ) : <EOL> return not ( not self . years and <EOL> not self . months and <EOL> not self . days and <EOL> not self . hours and <EOL> not self . minutes and <EOL> not self . seconds and <EOL> not self . microseconds and <EOL> not self . leapdays and <EOL> self . year is None and <EOL> self . month is None and <EOL> self . day is None and <EOL> self . weekday is None and <EOL> self . hour is None and <EOL> self . minute is None and <EOL> self . second is None and <EOL> self . microsecond is None ) <EOL> def __mul__ ( self , other ) : <EOL> f = float ( other ) <EOL> return relativedelta ( years = self . years * f , <EOL> months = self . months * f , <EOL> days = self . days * f , <EOL> hours = self . hours * f , <EOL> minutes = self . minutes * f , <EOL> seconds = self . seconds * f , <EOL> microseconds = self . microseconds * f , <EOL> leapdays = self . leapdays , <EOL> year = self . year , <EOL> month = self . month , <EOL> day = self . day , <EOL> weekday = self . weekday , <EOL> hour = self . hour , <EOL> minute = self . minute , <EOL> second = self . second , <EOL> microsecond = self . microsecond ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , relativedelta ) : <EOL> return False <EOL> if self . weekday or other . weekday : <EOL> if not self . weekday or not other . weekday : <EOL> return False <EOL> if self . weekday . weekday != other . weekday . weekday : <EOL> return False <EOL> n1 , n2 = self . weekday . n , other . weekday . n <EOL> if n1 != n2 and not ( ( not n1 or n1 == <NUM_LIT:1> ) and ( not n2 or n2 == <NUM_LIT:1> ) ) : <EOL> return False <EOL> return ( self . years == other . years and <EOL> self . months == other . months and <EOL> self . days == other . days and <EOL> self . hours == other . hours and <EOL> self . minutes == other . minutes and <EOL> self . seconds == other . seconds and <EOL> self . leapdays == other . leapdays and <EOL> self . year == other . year and <EOL> self . month == other . month and <EOL> self . day == other . day and <EOL> self . hour == other . hour and <EOL> self . minute == other . minute and <EOL> self . second == other . second and <EOL> self . microsecond == other . microsecond ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __div__ ( self , other ) : <EOL> return self . __mul__ ( <NUM_LIT:1> / float ( other ) ) <EOL> def __repr__ ( self ) : <EOL> l = [ ] <EOL> for attr in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> value = getattr ( self , attr ) <EOL> if value : <EOL> l . append ( "<STR_LIT>" % ( attr , value ) ) <EOL> for attr in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> value = getattr ( self , attr ) <EOL> if value is not None : <EOL> l . append ( "<STR_LIT>" % ( attr , ` value ` ) ) <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( l ) ) </s>
