def get_today(_user_profile, _format): """ user_start is today's midnight but it needs to be in user's TZ. user_stop is current time simply, in user's timezone again. """ user_now = now(timezone(_user_profile.timezone)).replace(tzinfo=None) user_today_midnight = datetime(user_now.year, user_now.month, user_now.day) utc_start = from_local_to_utc(user_today_midnight, _user_profile.timezone) utc_stop = from_local_to_utc(user_now, _user_profile.timezone) user_start = from_utc_to_user(utc_start, _user_profile, _format) user_stop = None return utc_start, utc_stop, user_start, user_stop
def shift(utc_base_date, user_start, user_profile, shift_type, duration, format): """ Shifts the base date by the amount specified and returns resulting start and stop dates in UTC and user timezone. """ if shift_type not in start_delta_kwargs: raise ValueError('Unknown shift_type:[{}]'.format(shift_type)) _start_delta_kwargs = start_delta_kwargs[shift_type] # Special-case month duration because UTC '2012-09-30 22:00:00+00:00' (which is 2012-10-01 CEST) # minus one month happens to be '2012-08-30 22:00:00+00:00' instead of '2012-09-31 22:00:00+00:00' # so it's 2012-08-30 CEST instead of 2012-09-01. In other words, we would've jumped from Oct 2012 to Aug 2012 directly. if duration != 'month': utc_start = utc_base_date + relativedelta(**_start_delta_kwargs) else: user_start = datetime.strptime(user_start, user_profile.month_year_format_strptime) current_month_start = datetime(user_start.year, user_start.month, 1) prev_month_start = current_month_start + relativedelta( **_start_delta_kwargs) utc_start = from_local_to_utc(prev_month_start, user_profile.timezone) _stop_delta_kwargs = stop_delta_kwargs[duration] utc_stop = utc_start + relativedelta(**_stop_delta_kwargs) user_start = from_utc_to_user(utc_start, user_profile, format) user_stop = from_utc_to_user(utc_stop, user_profile, format) return DateInfo(utc_start.isoformat(), utc_stop.isoformat(), user_start, user_stop)
def get_default_date(date_type, user_profile, format): """ Returns default start and stop date in UTC and user's timezone depending on the stats type and duration requested. """ def get_today(_user_profile, _format): """ user_start is today's midnight but it needs to be in user's TZ. user_stop is current time simply, in user's timezone again. """ user_now = now(timezone(_user_profile.timezone)).replace(tzinfo=None) user_today_midnight = datetime(user_now.year, user_now.month, user_now.day) utc_start = from_local_to_utc(user_today_midnight, _user_profile.timezone) utc_stop = from_local_to_utc(user_now, _user_profile.timezone) user_start = from_utc_to_user(utc_start, _user_profile, _format) user_stop = None return utc_start, utc_stop, user_start, user_stop if date_type == 'last_hour': # stop is what current time is now so return it in UTC and user's TZ # along with start which will be equal to stop - 1 hour. utc_stop = utc.fromutc(utcnow()) utc_start = utc.fromutc(utc_stop + relativedelta(hours=-1)) user_start = from_utc_to_user(utc_start, user_profile) user_stop = from_utc_to_user(utc_stop, user_profile) label = 'Last hour' step = 'hour' elif date_type == 'today': utc_start, utc_stop, user_start, user_stop = get_today( user_profile, format) label = 'Today' step = 'day' elif date_type == 'yesterday': # Yesterday's start is today's start - 1 day today_utc_start, today_utc_stop, today_user_start, user_stop = get_today( user_profile, format) utc_start = today_utc_start + relativedelta(days=-1) utc_stop = utc_start + relativedelta(days=1) user_start = from_utc_to_user(utc_start, user_profile, format) label = 'Yesterday' step = 'day' elif date_type == 'this_week': # This week extends from Monday midnight to right now user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None) user_prev_monday = user_now + relativedelta(weekday=MO(-1)) user_prev_monday = datetime(year=user_prev_monday.year, month=user_prev_monday.month, day=user_prev_monday.day) utc_start = from_local_to_utc(user_prev_monday, user_profile.timezone) utc_stop = from_local_to_utc(user_now, user_profile.timezone) user_start = from_utc_to_user(utc_start, user_profile, format) user_stop = from_utc_to_user(utc_stop, user_profile, format) label = 'This week' step = 'week' elif date_type == 'this_month': # From midnight the first day of month up until now user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None) user_1st_of_month = datetime(year=user_now.year, month=user_now.month, day=1) utc_start = from_local_to_utc(user_1st_of_month, user_profile.timezone) utc_stop = from_local_to_utc(user_now, user_profile.timezone) user_start = from_utc_to_user(utc_start, user_profile, format) user_stop = None label = 'This month' step = 'month' elif date_type == 'this_year': # From midnight the first day of year up until now user_now = now(timezone(user_profile.timezone)).replace(tzinfo=None) user_new_year = datetime(year=user_now.year, month=1, day=1) utc_start = from_local_to_utc(user_new_year, user_profile.timezone) utc_stop = from_local_to_utc(user_now, user_profile.timezone) user_start = from_utc_to_user(utc_start, user_profile, format) user_stop = None label = 'This year' step = 'year' else: raise ValueError('Unrecognized date_type:[{}]'.format(date_type)) return DateInfo(utc_start.isoformat(), utc_stop.isoformat(), user_start, user_stop, label, step)