<s> """<STR_LIT>""" <EOL> import re <EOL> from datetime import datetime , timedelta <EOL> STEP_10_SEC = long ( <NUM_LIT> ) <EOL> STEP_1_MIN = long ( <NUM_LIT> ) <EOL> STEP_5_MIN = long ( <NUM_LIT> ) <EOL> STEP_1_HOUR = long ( <NUM_LIT> ) <EOL> STEP_1_DAY = long ( <NUM_LIT> ) <EOL> STEP_CHOICES = ( ( STEP_10_SEC , "<STR_LIT>" ) , <EOL> ( STEP_1_MIN , "<STR_LIT>" ) , <EOL> ( STEP_5_MIN , "<STR_LIT>" ) , <EOL> ( STEP_1_HOUR , "<STR_LIT>" ) , <EOL> ( STEP_1_DAY , "<STR_LIT>" ) ) <EOL> def now ( ) : <EOL> return datetime . utcnow ( ) <EOL> def yesterday ( start = None ) : <EOL> if start is None : <EOL> start = now ( ) <EOL> return start - timedelta ( days = <NUM_LIT:1> ) <EOL> def last_week ( start = None ) : <EOL> if start is None : <EOL> start = now ( ) <EOL> return start - timedelta ( days = <NUM_LIT:7> ) <EOL> def start_of_month ( timestamp = None ) : <EOL> if not timestamp : <EOL> timestamp = now ( ) <EOL> return datetime ( year = timestamp . year , month = timestamp . month , day = <NUM_LIT:1> ) <EOL> def floor ( start , resolution ) : <EOL> """<STR_LIT>""" <EOL> if resolution == STEP_10_SEC : <EOL> return datetime ( start . year , start . month , start . day , start . hour , <EOL> start . minute , start . second - ( start . second % <NUM_LIT:10> ) ) <EOL> elif resolution == STEP_1_MIN : <EOL> return datetime ( start . year , start . month , start . day , start . hour , <EOL> start . minute ) <EOL> elif resolution == STEP_5_MIN : <EOL> return datetime ( start . year , start . month , start . day , start . hour , <EOL> start . minute - ( start . minute % <NUM_LIT:5> ) ) <EOL> elif resolution == STEP_1_HOUR : <EOL> return datetime ( start . year , start . month , start . day , start . hour ) <EOL> elif resolution == STEP_1_DAY : <EOL> return datetime ( start . year , start . month , start . day ) <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( resolution = resolution , <EOL> choices = STEP_CHOICES ) ) <EOL> def _timedelta_total_seconds ( td ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( timedelta , "<STR_LIT>" ) : <EOL> return getattr ( td , "<STR_LIT>" ) ( ) <EOL> return ( td . microseconds + ( td . seconds + td . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:10> ** <NUM_LIT:6> ) / float ( <NUM_LIT:10> ** <NUM_LIT:6> ) <EOL> def _interval_string_to_seconds ( interval_string ) : <EOL> """<STR_LIT>""" <EOL> interval_exc = "<STR_LIT>" . format ( interval_string ) <EOL> interval_dict = { "<STR_LIT:s>" : <NUM_LIT:1> , "<STR_LIT:m>" : <NUM_LIT> , "<STR_LIT:h>" : <NUM_LIT> , "<STR_LIT:D>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:7> * <NUM_LIT> , "<STR_LIT:M>" : <NUM_LIT:30> * <NUM_LIT> , "<STR_LIT:Y>" : <NUM_LIT> * <NUM_LIT> } <EOL> interval_regex = re . compile ( "<STR_LIT>" ) <EOL> seconds = <NUM_LIT:0> <EOL> while interval_string : <EOL> match = interval_regex . match ( interval_string ) <EOL> if match : <EOL> num , ext = int ( match . group ( "<STR_LIT>" ) ) , match . group ( "<STR_LIT>" ) <EOL> if num > <NUM_LIT:0> and ext in interval_dict : <EOL> seconds += num * interval_dict [ ext ] <EOL> interval_string = interval_string [ match . end ( ) : ] <EOL> else : <EOL> raise Exception ( interval_exc ) <EOL> else : <EOL> raise Exception ( interval_exc ) <EOL> return seconds <EOL> def timeago ( interval_string , start = None ) : <EOL> if start is None : <EOL> start = now ( ) <EOL> td = timedelta ( seconds = _interval_string_to_seconds ( interval_string ) ) <EOL> return start - td </s>
