def test_passing_tzinfo_to_job(self, job_queue): """Test that tzinfo is correctly passed to job with run_once, run_daily and run_repeating methods""" when_dt_tz_specific = dtm.datetime.now( tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) + dtm.timedelta( seconds=2) when_dt_tz_utc = dtm.datetime.now() + dtm.timedelta(seconds=2) job_once1 = job_queue.run_once(self.job_run_once, when_dt_tz_specific) job_once2 = job_queue.run_once(self.job_run_once, when_dt_tz_utc) when_time_tz_specific = ( dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) + dtm.timedelta(seconds=2)).timetz() when_time_tz_utc = (dtm.datetime.now() + dtm.timedelta(seconds=2)).timetz() job_once3 = job_queue.run_once(self.job_run_once, when_time_tz_specific) job_once4 = job_queue.run_once(self.job_run_once, when_time_tz_utc) first_dt_tz_specific = dtm.datetime.now( tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) + dtm.timedelta( seconds=2) first_dt_tz_utc = dtm.datetime.now() + dtm.timedelta(seconds=2) job_repeating1 = job_queue.run_repeating(self.job_run_once, 2, first=first_dt_tz_specific) job_repeating2 = job_queue.run_repeating(self.job_run_once, 2, first=first_dt_tz_utc) first_time_tz_specific = ( dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) + dtm.timedelta(seconds=2)).timetz() first_time_tz_utc = (dtm.datetime.now() + dtm.timedelta(seconds=2)).timetz() job_repeating3 = job_queue.run_repeating(self.job_run_once, 2, first=first_time_tz_specific) job_repeating4 = job_queue.run_repeating(self.job_run_once, 2, first=first_time_tz_utc) time_tz_specific = ( dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) + dtm.timedelta(seconds=2)).timetz() time_tz_utc = (dtm.datetime.now() + dtm.timedelta(seconds=2)).timetz() job_daily1 = job_queue.run_daily(self.job_run_once, time_tz_specific) job_daily2 = job_queue.run_daily(self.job_run_once, time_tz_utc) assert job_once1.tzinfo == when_dt_tz_specific.tzinfo assert job_once2.tzinfo == _UTC assert job_once3.tzinfo == when_time_tz_specific.tzinfo assert job_once4.tzinfo == _UTC assert job_repeating1.tzinfo == first_dt_tz_specific.tzinfo assert job_repeating2.tzinfo == _UTC assert job_repeating3.tzinfo == first_time_tz_specific.tzinfo assert job_repeating4.tzinfo == _UTC assert job_daily1.tzinfo == time_tz_specific.tzinfo assert job_daily2.tzinfo == _UTC
def test_job_set_next_t(self, job_queue): # Testing next_t setter for 'datetime.datetime' values job = job_queue.run_once(self.job_run_once, 0.05) t = dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=12))) job._set_next_t(t) job.tzinfo = _UtcOffsetTimezone(dtm.timedelta(hours=5)) assert job.next_t == t.astimezone(job.tzinfo)
def test_run_daily_with_timezone(self, job_queue): """test that the weekday is retrieved based on the job's timezone We set a job to run at the current UTC time of day (plus a small delay buffer) with a timezone that is---approximately (see below)---UTC +24, and set it to run on the weekday after the current UTC weekday. The job should therefore be executed now (because in UTC+24, the time of day is the same as the current weekday is the one after the current UTC weekday). """ now = time.time() utcnow = dtm.datetime.utcfromtimestamp(now) delta = 0.1 # must subtract one minute because the UTC offset has to be strictly less than 24h # thus this test will xpass if run in the interval [00:00, 00:01) UTC time # (because target time will be 23:59 UTC, so local and target weekday will be the same) target_tzinfo = _UtcOffsetTimezone(dtm.timedelta(days=1, minutes=-1)) target_datetime = (utcnow + dtm.timedelta( days=1, minutes=-1, seconds=delta)).replace(tzinfo=target_tzinfo) target_time = target_datetime.timetz() target_weekday = target_datetime.date().weekday() expected_reschedule_time = now + delta + 24 * 60 * 60 job_queue.run_daily(self.job_run_once, time=target_time, days=(target_weekday, )) sleep(delta + 0.1) assert self.result == 1 assert job_queue._queue.get(False)[0] == pytest.approx( expected_reschedule_time)
def timezone(utc_offset): return _UtcOffsetTimezone(utc_offset)
import time import datetime as dtm import pytest from telegram import Sticker from telegram import Update from telegram import User from telegram import MessageEntity from telegram.message import Message from telegram.utils import helpers from telegram.utils.helpers import _UtcOffsetTimezone, _datetime_to_float_timestamp # sample time specification values categorised into absolute / delta / time-of-day ABSOLUTE_TIME_SPECS = [dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=-7))), dtm.datetime.utcnow()] DELTA_TIME_SPECS = [dtm.timedelta(hours=3, seconds=42, milliseconds=2), 30, 7.5] TIME_OF_DAY_TIME_SPECS = [dtm.time(12, 42, tzinfo=_UtcOffsetTimezone(dtm.timedelta(hours=-7))), dtm.time(12, 42)] RELATIVE_TIME_SPECS = DELTA_TIME_SPECS + TIME_OF_DAY_TIME_SPECS TIME_SPECS = ABSOLUTE_TIME_SPECS + RELATIVE_TIME_SPECS class TestHelpers(object): def test_escape_markdown(self): test_str = '*bold*, _italic_, `code`, [text_link](http://github.com/)' expected_str = '\*bold\*, \_italic\_, \`code\`, \[text\_link](http://github.com/)' assert expected_str == helpers.escape_markdown(test_str)
# along with this program. If not, see [http://www.gnu.org/licenses/]. import time import datetime as dtm import pytest from telegram import Sticker from telegram import Update from telegram import User from telegram.message import Message from telegram.utils import helpers from telegram.utils.helpers import _UtcOffsetTimezone, _datetime_to_float_timestamp # sample time specification values categorised into absolute / delta / time-of-day ABSOLUTE_TIME_SPECS = [ dtm.datetime.now(tz=_UtcOffsetTimezone(dtm.timedelta(hours=-7))), dtm.datetime.utcnow() ] DELTA_TIME_SPECS = [ dtm.timedelta(hours=3, seconds=42, milliseconds=2), 30, 7.5 ] TIME_OF_DAY_TIME_SPECS = [ dtm.time(12, 42, tzinfo=_UtcOffsetTimezone(dtm.timedelta(hours=-7))), dtm.time(12, 42) ] RELATIVE_TIME_SPECS = DELTA_TIME_SPECS + TIME_OF_DAY_TIME_SPECS TIME_SPECS = ABSOLUTE_TIME_SPECS + RELATIVE_TIME_SPECS class TestHelpers(object): def test_escape_markdown(self):