<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from datetime import datetime , date , timedelta <EOL> from dateutil . relativedelta import relativedelta <EOL> import six <EOL> from croniter import croniter <EOL> cron_presets = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def date_range ( <EOL> start_date , <EOL> end_date = None , <EOL> num = None , <EOL> delta = None ) : <EOL> """<STR_LIT>""" <EOL> if not delta : <EOL> return [ ] <EOL> if end_date and start_date > end_date : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if end_date and num : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if not end_date and not num : <EOL> end_date = datetime . now ( ) <EOL> delta_iscron = False <EOL> if isinstance ( delta , six . string_types ) : <EOL> delta_iscron = True <EOL> cron = croniter ( delta , start_date ) <EOL> elif isinstance ( delta , timedelta ) : <EOL> delta = abs ( delta ) <EOL> l = [ ] <EOL> if end_date : <EOL> while start_date <= end_date : <EOL> l . append ( start_date ) <EOL> if delta_iscron : <EOL> start_date = cron . get_next ( datetime ) <EOL> else : <EOL> start_date += delta <EOL> else : <EOL> for i in range ( abs ( num ) ) : <EOL> l . append ( start_date ) <EOL> if delta_iscron : <EOL> if num > <NUM_LIT:0> : <EOL> start_date = cron . get_next ( datetime ) <EOL> else : <EOL> start_date = cron . get_prev ( datetime ) <EOL> else : <EOL> if num > <NUM_LIT:0> : <EOL> start_date += delta <EOL> else : <EOL> start_date -= delta <EOL> return sorted ( l ) <EOL> def round_time ( dt , delta , start_date = datetime . min ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( delta , six . string_types ) : <EOL> cron = croniter ( delta , start_date ) <EOL> prev = cron . get_prev ( datetime ) <EOL> if prev == start_date : <EOL> return start_date <EOL> else : <EOL> return prev <EOL> dt -= timedelta ( microseconds = dt . microsecond ) <EOL> upper = <NUM_LIT:1> <EOL> while start_date + upper * delta < dt : <EOL> upper *= <NUM_LIT:2> <EOL> lower = upper // <NUM_LIT:2> <EOL> while True : <EOL> if start_date + ( lower + <NUM_LIT:1> ) * delta >= dt : <EOL> if ( <EOL> ( start_date + ( lower + <NUM_LIT:1> ) * delta ) - dt <= <EOL> dt - ( start_date + lower * delta ) ) : <EOL> return start_date + ( lower + <NUM_LIT:1> ) * delta <EOL> else : <EOL> return start_date + lower * delta <EOL> candidate = lower + ( upper - lower ) // <NUM_LIT:2> <EOL> if start_date + candidate * delta >= dt : <EOL> upper = candidate <EOL> else : <EOL> lower = candidate </s>
