def get_interval_and_start_time(request): context = {} context = get_names_for_all_column_headers() move_interval = request.GET.get('move_interval', None) datetime_start = request.GET.get('datetime_start', None) interval = request.GET.get('interval', None) if not interval: interval = 10*60 #default interval is 10 minutes else: interval = int(interval)*60 #converting minutes (more suitable) to seconds if not datetime_start or datetime_start.lower() == 'now': datetime_start = utils.strftime(utils.get_local_now(), "%Y-%m-%d %H:%M:%S") start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_start, "%Y-%m-%d %H:%M:%S")) if move_interval and move_interval == 'back': start_timestamp -= interval elif move_interval and move_interval == 'forward': start_timestamp += interval context['datetime_start'] = datetime_start context['start_timestamp'] = start_timestamp context['interval'] = interval return context
def set_time_parameters(start_timestamp, end_timestamp, datetime_from, datetime_to, interval, move_interval): # dictionary used to update time fields in form submited with back or forward buttons update_form_time = {} update_form_time['datetime_start'] = None update_form_time['datetime_end'] = None update_form_time['start_timestamp'] = None update_form_time['end_timestamp'] = None # A good URL has start and end_timestamp filled in. # If not, we proceed through a hiearchy to determine the time to use if start_timestamp and end_timestamp: update_form_time['start_timestamp'] = start_timestamp start_timestamp = int(start_timestamp) update_form_time['end_timestamp'] = end_timestamp end_timestamp = int(end_timestamp) else: # data type conversions if start_timestamp: update_form_time['start_timestamp'] = start_timestamp start_timestamp = int(start_timestamp) if end_timestamp: update_form_time['end_timestamp'] = end_timestamp if end_timestamp.lower() == "now": end_timestamp = int(time.time()) # set end_timestamp to now else: end_timestamp = int(end_timestamp) if interval: interval = int(interval) if datetime_to.lower() == "now": datetime_to = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # set datetime_to to current datetime # datetime format trumps UNIX timestamp format if both are given # datetimes if datetime_from and datetime_to: start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_from, '%Y-%m-%d %H:%M:%S')) end_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_to, '%Y-%m-%d %H:%M:%S')) elif datetime_from and interval: update_form_time['datetime_start'] = datetime_from start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_from, '%Y-%m-%d %H:%M:%S')) end_timestamp = start_timestamp + interval elif datetime_to and interval: update_form_time['datetime_end'] = datetime_to end_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_to, '%Y-%m-%d %H:%M:%S')) start_timestamp = end_timestamp - interval # UNIX timestamps elif start_timestamp and end_timestamp: pass elif start_timestamp and interval: end_timestamp = start_timestamp + interval elif end_timestamp and interval: start_timestamp = end_timestamp - interval else: return (0,0) #return HttpResponseBadRequest("Please double check your date/time values.") # this doesn't work - what to do instead? # back or forward buttons interval = end_timestamp - start_timestamp # TOOD: get rid of and interval once i determine how interval is taken care of if move_interval and interval: if move_interval == 'back': start_timestamp -= interval end_timestamp -= interval elif move_interval == 'forward': start_timestamp += interval end_timestamp += interval if update_form_time['start_timestamp'] and update_form_time['end_timestamp']: update_form_time['start_timestamp'] = start_timestamp update_form_time['end_timestamp'] = end_timestamp else: if update_form_time['datetime_start'] and interval: update_form_time['datetime_start'] = datetime.datetime.fromtimestamp(start_timestamp).strftime('%Y-%m-%d %H:%M:%S') elif update_form_time['datetime_end'] and interval: update_form_time['datetime_end'] = datetime.datetime.fromtimestamp(end_timestamp).strftime('%Y-%m-%d %H:%M:%S') elif update_form_time['start_timestamp'] and interval: update_form_time['start_timestamp'] = start_timestamp elif update_form_time['end_timestamp'] and interval: update_form_time['end_timestamp'] = end_timestamp return (start_timestamp, end_timestamp, update_form_time)