def test_shift_prev_day(self): with patch('zato.common.util._utcnow', self._utcnow): now = utcnow() info = shift(now, from_utc_to_user(now, self.user_profile), self.user_profile, 'today_prev_day', 'hour', 'date_time') eq_(info.utc_start, '2012-02-29T00:47:24.054903+00:00') eq_(info.utc_stop, '2012-02-29T01:47:24.054903+00:00') eq_(info.user_start, '29-02-2012 01:47:24') eq_(info.user_stop, '29-02-2012 02:47:24') eq_(info.step, None)
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)