freezegun
freezegun copied to clipboard
Respect System Timezone
This fails a ton of built in tests - not sure anyway wants to use it or do the work to make tests pass.
It works for us though - we're forking it internally - thought maybe someone else could find it useful? We're using Django which patches the system TZ. Googlers - I hope you find out why your freezegun is working incorrectly or showing the wrong time with Django!
Here is an example of a test that worked for us:
import pytz
import time
from datetime import datetime, date
from django.test import TestCase
from freezegun import freeze_time
class FreezeTimeOverrideTest(TestCase):
def test_now_with_utc_dance(self):
now = datetime.now()
time_time = int(time.time())
time_localtime = time.localtime()
time_gmtime = time.gmtime()
date_today = date.today()
datetime_now = now.replace(microsecond=0)
datetime_now_pytz_UTC = datetime.now(pytz.UTC).replace(microsecond=0)
datetime_utcnow = datetime.utcnow().replace(microsecond=0)
datetime_now_pytz_timezone_America_Los_Angeles = datetime.now(pytz.timezone('America/Los_Angeles')).replace(microsecond=0)
time.sleep(1.2)
# CST from django system override
freeze_now = now.replace(microsecond=0).isoformat()
del now
with freeze_time(freeze_now):
self.assertEquals(
int(time.time()),
time_time
)
self.assertEquals(
list(time.localtime())[:-1], # isdst is wrong...
list(time_localtime)[:-1] # isdst is wrong...
)
self.assertEquals(
list(time.gmtime())[:-1], # isdst is wrong...
list(time_gmtime)[:-1] # isdst is wrong...
)
self.assertEquals(
date.today(),
date_today
)
self.assertEquals(
datetime.now().replace(microsecond=0),
datetime_now
)
self.assertEquals(
datetime.now(pytz.UTC).replace(microsecond=0),
datetime_now_pytz_UTC
)
self.assertEquals(
datetime.utcnow().replace(microsecond=0),
datetime_utcnow
)
self.assertEquals(
datetime.now(pytz.timezone('America/Los_Angeles')).replace(microsecond=0),
datetime_now_pytz_timezone_America_Los_Angeles
)
isdst is currently wrong too - haven't figured that one out. The "fix" is really inelegant as well. :shrugs:
Mainly putting it out there just in case someone else hits the same thing.
Any updated on this? We're seeing some breakage downstream because of this issue.
No update from me. Would be open to someone taking this and running with it.
Am I reading this PR correctly in that it wants to make tests with frozen time not freeze to a predictable time but to whatever local time zone is on the system?
If that's the case then it seems pretty obvious to me why this would fail all the tests: it's backwards incompatible and would result in flaky tests if freezegun would to have these changes.