tz_offset and datetime.now(utc) don't interact correctly
See original GitHub issueWhen debugging logging timestamps inside freezegun, noticed odd behaviour around calling datetime.now(timezone.utc)
when freeze_time
is active with a tz_offset
set:
import datetime
import freezegun
import logging
import sys
import time
def log_times(name):
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
handler=logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(
logging.Formatter(
fmt=f"%(name)s %(asctime)s - %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z",
)
)
logger.addHandler(handler)
logger.info("now(): %s", datetime.datetime.now().isoformat(timespec='seconds'))
logger.info("utcnow(): %s", datetime.datetime.utcnow().isoformat(timespec='seconds'))
logger.info("now(utc): %s", datetime.datetime.now(datetime.timezone.utc).isoformat(timespec='seconds'))
logger.info("now(local): %s", datetime.datetime.now().astimezone().isoformat(timespec='seconds'))
logger.info("time.time(): %s", time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time())))
print(sys.version)
print(freezegun.__version__)
log_times("native")
with freezegun.freeze_time(datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0), tz_offset=0):
log_times("offset0")
with freezegun.freeze_time(datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0), tz_offset=1):
log_times("offset1")
Output:
3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)]
0.3.15
native 2020-05-01T15:59:53+0100 - now(): 2020-05-01T15:59:53
native 2020-05-01T15:59:53+0100 - utcnow(): 2020-05-01T14:59:53
native 2020-05-01T15:59:53+0100 - now(utc): 2020-05-01T14:59:53+00:00
native 2020-05-01T15:59:53+0100 - now(local): 2020-05-01T15:59:53+01:00
native 2020-05-01T15:59:53+0100 - time.time(): 2020-05-01T15:59:53+0100
offset0 2020-05-01T15:59:53+0100 - now(): 2020-05-01T14:59:53
offset0 2020-05-01T15:59:53+0100 - utcnow(): 2020-05-01T14:59:53
offset0 2020-05-01T15:59:53+0100 - now(utc): 2020-05-01T14:59:53+00:00
offset0 2020-05-01T15:59:53+0100 - now(local): 2020-05-01T14:59:53+01:00
offset0 2020-05-01T15:59:53+0100 - time.time(): 2020-05-01T15:59:53+0100
offset1 2020-05-01T15:59:53+0100 - now(): 2020-05-01T15:59:53
offset1 2020-05-01T15:59:53+0100 - utcnow(): 2020-05-01T14:59:53
offset1 2020-05-01T15:59:53+0100 - now(utc): 2020-05-01T15:59:53+00:00
offset1 2020-05-01T15:59:53+0100 - now(local): 2020-05-01T15:59:53+01:00
offset1 2020-05-01T15:59:53+0100 - time.time(): 2020-05-01T15:59:53+0100
Because of https://github.com/spulec/freezegun/issues/204, the logtimes are always localtime, which is currently +0100, correctly reflected in ‘native’.
My understanding is that ‘offset0’ (tz_offset=0
) should be reflecting a frozen time on the UTC timezone, barring existing bugs that attempt to load system timezone, reflected in the time.time()
line.
I believe ‘offset1’(tz_offset=1
) then should be a frozen time on a timezone with the same delta from UTC (+0100), which is correct except for the now(utc)
line? Which should just be the same line as utcnow()
but with a +0000 timezone attached, as seen on ‘native’.
Not sure if this is the same as other bugs reflecting that the system timezone leaks into the freeze if unspecified, but I’m specifying an explicit timezone during the freeze, which doesn’t seem exactly the same.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:10 (1 by maintainers)
This patch works for me! This is the test case:
Nope. I saw your last activity in commits and offer fix this issue cause here (in this issue) have problematic code example (test suite) and fix (my workaround)