def test_to_float_timestamp_time_of_day(self): """Conversion from time-of-day specification to timestamp""" hour, hour_delta = 12, 1 ref_t = _datetime_to_float_timestamp(dtm.datetime(1970, 1, 1, hour=hour)) # test for a time of day that is still to come, and one in the past time_future, time_past = dtm.time(hour + hour_delta), dtm.time(hour - hour_delta) assert helpers.to_float_timestamp(time_future, ref_t) == ref_t + 60 * 60 * hour_delta assert helpers.to_float_timestamp(time_past, ref_t) == ref_t + 60 * 60 * (24 - hour_delta)
def test_to_float_timestamp_time_of_day_timezone(self, timezone): """Conversion from timezone-aware time-of-day specification to timestamp""" # we're parametrizing this with two different UTC offsets to exclude the possibility # of an xpass when the test is run in a timezone with the same UTC offset utc_offset = timezone.utcoffset(None) ref_datetime = dtm.datetime(1970, 1, 1, 12) ref_t, time_of_day = _datetime_to_float_timestamp(ref_datetime), ref_datetime.time() # first test that naive time is assumed to be utc: assert helpers.to_float_timestamp(time_of_day, ref_t) == pytest.approx(ref_t) # test that by setting the timezone the timestamp changes accordingly: assert (helpers.to_float_timestamp(time_of_day.replace(tzinfo=timezone), ref_t) == pytest.approx(ref_t + (-utc_offset.total_seconds() % (24 * 60 * 60))))
def _put(self, job, time_spec=None, previous_t=None): """ Enqueues the job, scheduling its next run at the correct time. Args: job (telegram.ext.Job): job to enqueue time_spec (optional): Specification of the time for which the job should be scheduled. The precise semantics of this parameter depend on its type (see :func:`telegram.ext.JobQueue.run_repeating` for details). Defaults to now + ``job.interval``. previous_t (optional): Time at which the job last ran (``None`` if it hasn't run yet). """ # get time at which to run: if time_spec is None: time_spec = job.interval if time_spec is None: raise ValueError( "no time specification given for scheduling non-repeating job") next_t = to_float_timestamp(time_spec, reference_timestamp=previous_t) # enqueue: self.logger.debug('Putting job %s with t=%s', job.name, time_spec) self._queue.put((next_t, job)) job._set_next_t(next_t) # Wake up the loop if this job should be executed next self._set_next_peek(next_t)
def test_to_float_timestamp_delta(self, time_spec): """Conversion from a 'delta' time specification to timestamp""" reference_t = 0 delta = time_spec.total_seconds() if hasattr( time_spec, 'total_seconds') else time_spec assert helpers.to_float_timestamp(time_spec, reference_t) == reference_t + delta
def test_to_float_timestamp_absolute_naive_no_pytz(self, monkeypatch): """Conversion from timezone-naive datetime to timestamp. Naive datetimes should be assumed to be in UTC. """ monkeypatch.setattr(helpers, 'UTC', helpers.DTM_UTC) datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5) assert helpers.to_float_timestamp(datetime) == 1573431976.1
def test_to_float_timestamp_absolute_aware(self, timezone): """Conversion from timezone-aware datetime to timestamp""" # we're parametrizing this with two different UTC offsets to exclude the possibility # of an xpass when the test is run in a timezone with the same UTC offset datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5, tzinfo=timezone) assert (helpers.to_float_timestamp(datetime) == 1573431976.1 - timezone.utcoffset(None).total_seconds())
def _set_next_t(self, next_t): if isinstance(next_t, datetime.datetime): # Set timezone to UTC in case datetime is in local timezone. next_t = next_t.astimezone(datetime.timezone.utc) next_t = to_float_timestamp(next_t) elif not (isinstance(next_t, Number) or next_t is None): raise TypeError("The 'next_t' argument should be one of the following types: " "'float', 'int', 'datetime.datetime' or 'NoneType'") self._next_t = next_t
def __clear(self, mapping: MutableMapping, time_cutoff: Union[float, datetime] = None) -> None: if not time_cutoff: mapping.clear() return if isinstance(time_cutoff, datetime): effective_cutoff = to_float_timestamp( time_cutoff, tzinfo=self.bot.defaults.tzinfo if self.bot.defaults else None) else: effective_cutoff = time_cutoff # We need a list instead of a generator here, as the list doesn't change it's size # during the iteration to_drop = [ key for key, data in mapping.items() if data.access_time < effective_cutoff ] for key in to_drop: mapping.pop(key)
def test_to_timestamp(self, time_spec): # delegate tests to `to_float_timestamp` assert helpers.to_timestamp(time_spec) == int( helpers.to_float_timestamp(time_spec))
def test_to_float_timestamp_error(self): with pytest.raises(TypeError, match='Defaults'): helpers.to_float_timestamp(Defaults())
def test_to_float_timestamp_default_reference(self, time_spec): """The reference timestamp for relative time specifications should default to now""" now = time.time() assert helpers.to_float_timestamp(time_spec) == pytest.approx( helpers.to_float_timestamp(time_spec, reference_timestamp=now))
def test_to_float_timestamp_absolute_no_reference(self): """A reference timestamp is only relevant for relative time specifications""" with pytest.raises(ValueError): helpers.to_float_timestamp(dtm.datetime(2019, 11, 11), reference_timestamp=123)
def test_to_float_timestamp_absolute_naive(self): """Conversion from timezone-naive datetime to timestamp. Naive datetimes should be assumed to be in UTC. """ datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5) assert helpers.to_float_timestamp(datetime) == 1573431976.1