def test_tick(): with immobilus('2019-08-21 12:00:00') as dt: assert datetime(2019, 8, 21, 12, 0, 0) == datetime.now() dt.tick() assert datetime(2019, 8, 21, 12, 0, 1) == datetime.now() dt.tick(timedelta(seconds=10)) assert datetime(2019, 8, 21, 12, 0, 11) == datetime.now()
def test_mktime_without_timezone(): with immobilus('1970-01-01 00:00:00'): timetuple = datetime.now().timetuple() timestamp = datetime_to_timestamp(datetime.now()) mktime = time.mktime(timetuple) assert mktime == timestamp
def test_without_offset(): with immobilus('1970-01-01 00:00:00'): timestamp = _datetime_to_utc_timestamp(datetime.utcnow()) timetuple = datetime.utcnow().timetuple() mktime = time.mktime(timetuple) assert mktime == timestamp
def test_transmutations(): with immobilus('2017-01-01 00:00:00', tz_offset=2): now = datetime.now() timetuple = now.timetuple() mktime = time.mktime(timetuple) fromtimestamp = datetime.fromtimestamp(mktime) assert now.replace(microsecond=0) == fromtimestamp
def test_with_negative_offset(): # UTC (gmt+0) 06:00, EST (gmt-5) 01:00 with immobilus('1970-01-01 06:00:00', tz_offset=-5): timestamp = _datetime_to_utc_timestamp( datetime.utcnow()) # utc time, 06:00 timetuple = datetime.now().timetuple() # local time, 01:00 mktime = time.mktime(timetuple) assert mktime == timestamp == 6 * 3600
def test_with_positive_offset(): # UTC (gmt+0) 03:00, Moscow (gmt+3) 06:00 with immobilus('1970-01-01 03:00:00', tz_offset=3): timestamp = _datetime_to_utc_timestamp( datetime.utcnow()) # utc time, 03:00 timetuple = datetime.now().timetuple() # local time, 06:00 mktime = time.mktime(timetuple) assert mktime == timestamp == 3 * 3600
def test_mktime_gets_timezone_from_timetuple(): timezone = pytz.timezone('US/Eastern') dt = datetime(1970, 1, 1, 0, 0, tzinfo=timezone) with immobilus(dt): timetuple = datetime.now().timetuple() timestamp = datetime_to_timestamp(datetime.now()) mktime = time.mktime(timetuple) assert mktime == timestamp