I'm puzzled why a function that freezes time with freezegun outputs different UTC times depending on whether datetime.datetime.utcnow()
is called, or datetime.datetime.now(pytz.utc)
. I'm not saying it's broken, just that I don't understand why, and would like to know!
eg, using this function:
@freeze_time("2012-01-14 03:21:34", tz_offset=-4)
def test():
print("utcnow(): %s" % datetime.datetime.utcnow())
print("pytz.utc: %s" % datetime.datetime.now(pytz.utc))
the output is:
utcnow(): 2012-01-14 03:21:34
pytz.utc: 2012-01-13 23:21:34+00:00
I guess the first is a naive datetime, but why are they different times?
(Ultimately why I want to know: if I'm using freezegun in my tests, and I use pytz to generate times in my code being tested, I want to know what its 'correct' behaviour should be.)
tz_offset=-4
? One is aware the other is naive – Leucippusdatetime.datetime.now()
in that function you get2012-01-13 23:21:34
. Is freezegun freezing the time to 2012-01-14 03:21 UTC or 2012-01-13 23:21 UTC? – Fruindatetime.datetime.now()
is the local time (ie, 2012-01-13 23:21) and I'm artificially forcing that time to have UTC timezone info? – Fruin2012-01-13 23:21)
is the local time, it is also shown in the docs under timezones github.com/spulec/freezegun although the assert would fail for datetime.today because the time is not in datetime.today so it would be00:00:00
fordatetime.datetime(2012, 01, 13)
. If you useassert datetime.date.today() == datetime.datetime(2012, 01, 13).date()
it should pass – Leucippus2012-01-14 03:21:34+00:00
.23:21:34+00:00
looks like a bug. – Rancor03:21:34+00:00
? – Leucippusutcnow() == now(pytz.utc).replace(tzinfo=None)
? – Rancorpytz.utc
is being ignored? – Leucippuspytz.utc
like that doesn't affect the calculation of the time value - it simply takes the time thatnow()
generated and gives it the UTC timezone, without actually changing the time. I may be wrong, but that's what I'm assuming is happening. So the docs are correct. – Fruinutcnow() == 03:21:34
and thereforenow(pytz.utc)
must be03:21:34+00:00
. – Rancor