def remaining_estimate(self, last_run_at): """Return estimate of next time to run. Returns: ~datetime.timedelta: when the periodic task should run next, or if it shouldn't run today (e.g., the sun does not rise today), returns the time when the next check should take place. """ last_run_at = self.maybe_make_aware(last_run_at) last_run_at_utc = localize(last_run_at, timezone.utc) self.cal.date = last_run_at_utc try: if self.use_center: next_utc = getattr(self.cal, self.method)(self.ephem.Sun(), start=last_run_at_utc, use_center=self.use_center) else: next_utc = getattr(self.cal, self.method)(self.ephem.Sun(), start=last_run_at_utc) except self.ephem.CircumpolarError: # pragma: no cover # Sun won't rise/set today. Check again tomorrow # (specifically, after the next anti-transit). next_utc = (self.cal.next_antitransit(self.ephem.Sun()) + timedelta(minutes=1)) next = self.maybe_make_aware(next_utc.datetime()) now = self.maybe_make_aware(self.now()) delta = next - now return delta
def test_now(self): timezone_setting_value = 'US/Eastern' tz_utc = timezone.get_timezone('UTC') tz_us_eastern = timezone.get_timezone(timezone_setting_value) now = to_utc(datetime.utcnow()) app_now = self.app.now() assert app_now.tzinfo is tz_utc assert app_now - now <= timedelta(seconds=1) # Check that timezone conversion is applied from configuration self.app.conf.enable_utc = False self.app.conf.timezone = timezone_setting_value # timezone is a cached property del self.app.timezone app_now = self.app.now() assert app_now.tzinfo.zone == tz_us_eastern.zone diff = to_utc(datetime.utcnow()) - localize(app_now, tz_utc) assert diff <= timedelta(seconds=1) # Verify that timezone setting overrides enable_utc=on setting self.app.conf.enable_utc = True del self.app.timezone app_now = self.app.now() assert self.app.timezone == tz_us_eastern assert app_now.tzinfo.zone == tz_us_eastern.zone
def test_tz_without_normalize(self): class tzz(tzinfo): def utcoffset(self, dt): return None # Mock no utcoffset specified tz = tzz() assert not hasattr(tz, 'normalize') assert localize(make_aware(datetime.utcnow(), tz), tz)
def test_when_has_normalize(self): class tzz(tzinfo): raises = None def utcoffset(self, dt): return None def normalize(self, dt, **kwargs): self.normalized = True if self.raises and kwargs and kwargs.get('is_dst') is None: self.raised = True raise self.raises return 1 # needed by min() in Python 3 (None not hashable) tz = tzz() localize(make_aware(datetime.utcnow(), tz), tz) assert tz.normalized tz2 = tzz() tz2.raises = AmbiguousTimeError() localize(make_aware(datetime.utcnow(), tz2), tz2) assert tz2.normalized assert tz2.raised tz3 = tzz() tz3.raises = TypeError() localize(make_aware(datetime.utcnow(), tz3), tz3) assert tz3.normalized assert tz3.raised
def test_when_has_normalize(self): class tzz(tzinfo): raises = None def normalize(self, dt, **kwargs): self.normalized = True if self.raises and kwargs and kwargs.get('is_dst') is None: self.raised = True raise self.raises return 1 # needed by min() in Python 3 (None not hashable) tz = tzz() localize(make_aware(datetime.utcnow(), tz), tz) assert tz.normalized tz2 = tzz() tz2.raises = AmbiguousTimeError() localize(make_aware(datetime.utcnow(), tz2), tz2) assert tz2.normalized assert tz2.raised tz3 = tzz() tz3.raises = TypeError() localize(make_aware(datetime.utcnow(), tz3), tz3) assert tz3.normalized assert tz3.raised
def test_localize_aware_dt_idempotent(self): t = (2017, 4, 23, 21, 36, 59, 0) local_zone = pytz.timezone('America/New_York') local_time = datetime(*t) local_time_aware = datetime(*t, tzinfo=local_zone) alternate_zone = pytz.timezone('America/Detroit') localized_time = localize(local_time_aware, alternate_zone) assert localized_time == local_time_aware assert local_zone.utcoffset(local_time) == alternate_zone.utcoffset( local_time) localized_utc_offset = localized_time.tzinfo.utcoffset(local_time) assert localized_utc_offset == alternate_zone.utcoffset(local_time) assert localized_utc_offset == local_zone.utcoffset(local_time)
def test_localize_aware_dt_idempotent(self): t = (2017, 4, 23, 21, 36, 59, 0) local_zone = pytz.timezone('America/New_York') local_time = datetime(*t) local_time_aware = datetime(*t, tzinfo=local_zone) alternate_zone = pytz.timezone('America/Detroit') localized_time = localize(local_time_aware, alternate_zone) assert localized_time == local_time_aware assert local_zone.utcoffset( local_time) == alternate_zone.utcoffset(local_time) localized_utc_offset = localized_time.tzinfo.utcoffset(local_time) assert localized_utc_offset == alternate_zone.utcoffset(local_time) assert localized_utc_offset == local_zone.utcoffset(local_time)
def test_localize_changes_utc_dt(self): now_utc_time = datetime.now(tz=pytz.utc) local_tz = pytz.timezone('US/Eastern') localized_time = localize(now_utc_time, local_tz) assert localized_time == now_utc_time
def test_tz_without_normalize(self): tz = tzinfo() assert not hasattr(tz, 'normalize') assert localize(make_aware(datetime.utcnow(), tz), tz)
def test_tz_without_normalize(self): tz = tzinfo() self.assertFalse(hasattr(tz, 'normalize')) self.assertTrue(localize(make_aware(datetime.utcnow(), tz), tz))
def calc_timeout(options): countdown, eta = options.get("countdown"), options.get("eta") if countdown: return countdown elif eta: return localize(eta, timezone.utc)