<s> """<STR_LIT>""" <EOL> import calendar <EOL> import datetime <EOL> import time <EOL> import iso8601 <EOL> import six <EOL> _ISO8601_TIME_FORMAT_SUBSECOND = '<STR_LIT>' <EOL> _ISO8601_TIME_FORMAT = '<STR_LIT>' <EOL> PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND <EOL> def isotime ( at = None , subsecond = False ) : <EOL> """<STR_LIT>""" <EOL> if not at : <EOL> at = utcnow ( ) <EOL> st = at . strftime ( _ISO8601_TIME_FORMAT <EOL> if not subsecond <EOL> else _ISO8601_TIME_FORMAT_SUBSECOND ) <EOL> tz = at . tzinfo . tzname ( None ) if at . tzinfo else '<STR_LIT>' <EOL> st += ( '<STR_LIT>' if tz == '<STR_LIT>' else tz ) <EOL> return st <EOL> def parse_isotime ( timestr ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return iso8601 . parse_date ( timestr ) <EOL> except iso8601 . ParseError as e : <EOL> raise ValueError ( six . text_type ( e ) ) <EOL> except TypeError as e : <EOL> raise ValueError ( six . text_type ( e ) ) <EOL> def strtime ( at = None , fmt = PERFECT_TIME_FORMAT ) : <EOL> """<STR_LIT>""" <EOL> if not at : <EOL> at = utcnow ( ) <EOL> return at . strftime ( fmt ) <EOL> def parse_strtime ( timestr , fmt = PERFECT_TIME_FORMAT ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . strptime ( timestr , fmt ) <EOL> def normalize_time ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> offset = timestamp . utcoffset ( ) <EOL> if offset is None : <EOL> return timestamp <EOL> return timestamp . replace ( tzinfo = None ) - offset <EOL> def is_older_than ( before , seconds ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( before , six . string_types ) : <EOL> before = parse_strtime ( before ) . replace ( tzinfo = None ) <EOL> else : <EOL> before = before . replace ( tzinfo = None ) <EOL> return utcnow ( ) - before > datetime . timedelta ( seconds = seconds ) <EOL> def is_newer_than ( after , seconds ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( after , six . string_types ) : <EOL> after = parse_strtime ( after ) . replace ( tzinfo = None ) <EOL> else : <EOL> after = after . replace ( tzinfo = None ) <EOL> return after - utcnow ( ) > datetime . timedelta ( seconds = seconds ) <EOL> def utcnow_ts ( ) : <EOL> """<STR_LIT>""" <EOL> if utcnow . override_time is None : <EOL> return int ( time . time ( ) ) <EOL> return calendar . timegm ( utcnow ( ) . timetuple ( ) ) <EOL> def utcnow ( ) : <EOL> """<STR_LIT>""" <EOL> if utcnow . override_time : <EOL> try : <EOL> return utcnow . override_time . pop ( <NUM_LIT:0> ) <EOL> except AttributeError : <EOL> return utcnow . override_time <EOL> return datetime . datetime . utcnow ( ) <EOL> def iso8601_from_timestamp ( timestamp ) : <EOL> """<STR_LIT>""" <EOL> return isotime ( datetime . datetime . utcfromtimestamp ( timestamp ) ) <EOL> utcnow . override_time = None <EOL> def set_time_override ( override_time = None ) : <EOL> """<STR_LIT>""" <EOL> utcnow . override_time = override_time or datetime . datetime . utcnow ( ) <EOL> def advance_time_delta ( timedelta ) : <EOL> """<STR_LIT>""" <EOL> assert utcnow . override_time is not None <EOL> try : <EOL> for dt in utcnow . override_time : <EOL> dt += timedelta <EOL> except TypeError : <EOL> utcnow . override_time += timedelta <EOL> def advance_time_seconds ( seconds ) : <EOL> """<STR_LIT>""" <EOL> advance_time_delta ( datetime . timedelta ( <NUM_LIT:0> , seconds ) ) <EOL> def clear_time_override ( ) : <EOL> """<STR_LIT>""" <EOL> utcnow . override_time = None <EOL> def marshall_now ( now = None ) : <EOL> """<STR_LIT>""" <EOL> if not now : <EOL> now = utcnow ( ) <EOL> return dict ( day = now . day , month = now . month , year = now . year , hour = now . hour , <EOL> minute = now . minute , second = now . second , <EOL> microsecond = now . microsecond ) <EOL> def unmarshall_time ( tyme ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime ( day = tyme [ '<STR_LIT>' ] , <EOL> month = tyme [ '<STR_LIT>' ] , <EOL> year = tyme [ '<STR_LIT>' ] , <EOL> hour = tyme [ '<STR_LIT>' ] , <EOL> minute = tyme [ '<STR_LIT>' ] , <EOL> second = tyme [ '<STR_LIT>' ] , <EOL> microsecond = tyme [ '<STR_LIT>' ] ) <EOL> def delta_seconds ( before , after ) : <EOL> """<STR_LIT>""" <EOL> delta = after - before <EOL> return total_seconds ( delta ) <EOL> def total_seconds ( delta ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return delta . total_seconds ( ) <EOL> except AttributeError : <EOL> return ( ( delta . days * <NUM_LIT> * <NUM_LIT> ) + delta . seconds + <EOL> float ( delta . microseconds ) / ( <NUM_LIT:10> ** <NUM_LIT:6> ) ) <EOL> def is_soon ( dt , window ) : <EOL> """<STR_LIT>""" <EOL> soon = ( utcnow ( ) + datetime . timedelta ( seconds = window ) ) <EOL> return normalize_time ( dt ) <= soon </s>
