This is my code for testing the role of TIME_ZONE
and `USE_TZ:
from django.test.utils import override_settings
class TimeZoneTest(TestCase):
@override_settings(TIME_ZONE='UTC', USE_TZ=True)
@freeze_time("2018-01-04 13:30:55", tz_offset=9) # The reason that I add tz_offset=9 is to make local time samw with `Asia/Seoul`
def test_freeze_time1(self):
print("TIME_ZONE='UTC', USE_TZ=True")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='UTC', USE_TZ=False)
@freeze_time("2018-01-04 13:30:55", tz_offset=9)
def test_freeze_time2(self):
print("TIME_ZONE='UTC', USE_TZ=False")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='Asia/Seoul', USE_TZ=True)
@freeze_time("2018-01-04 13:30:55", tz_offset=9)
def test_freeze_time3(self):
print("TIME_ZONE='Asia/Seoul', USE_TZ=True")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='Asia/Seoul', USE_TZ=False)
@freeze_time("2018-01-04 13:30:55", tz_offset=9)
def test_freeze_time4(self):
print("TIME_ZONE='Asia/Seoul', USE_TZ=False")
print(datetime.now())
print(timezone.now())
Result
TIME_ZONE='UTC', USE_TZ=True
2018-01-04 22:30:55
2018-01-04 13:30:55+00:00
.TIME_ZONE='UTC', USE_TZ=False
2018-01-04 22:30:55
2018-01-04 22:30:55
.TIME_ZONE='Asia/Seoul', USE_TZ=True
2018-01-04 22:30:55
2018-01-04 13:30:55+00:00
.TIME_ZONE='Asia/Seoul', USE_TZ=False
2018-01-04 22:30:55
2018-01-04 22:30:55
The strange part is test_freeze_time1()
, which tests under TIME_ZONE='UTC', USE_TZ=True
, the reason that I think it strange is that datetime.now()
doesn't print out UTC
time, which is not True if I removed @freeze_time("2018-01-04 13:30:55", tz_offset=9)
:
from django.test.utils import override_settings
class TimeZoneTest(TestCase):
@override_settings(TIME_ZONE='UTC', USE_TZ=True)
def test_freeze_time1(self):
print("TIME_ZONE='UTC', USE_TZ=True")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='UTC', USE_TZ=False)
def test_freeze_time2(self):
print("TIME_ZONE='UTC', USE_TZ=False")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='Asia/Seoul', USE_TZ=True)
def test_freeze_time3(self):
print("TIME_ZONE='Asia/Seoul', USE_TZ=True")
print(datetime.now())
print(timezone.now())
@override_settings(TIME_ZONE='Asia/Seoul', USE_TZ=False)
def test_freeze_time4(self):
print("TIME_ZONE='Asia/Seoul', USE_TZ=False")
print(datetime.now())
print(timezone.now())
Result
TIME_ZONE='UTC', USE_TZ=True
2018-02-13 09:10:49.715005
2018-02-13 09:10:49.715018+00:00
.TIME_ZONE='UTC', USE_TZ=False
2018-02-13 09:10:49.715970
2018-02-13 09:10:49.715980
.TIME_ZONE='Asia/Seoul', USE_TZ=True
2018-02-13 18:10:49.716758
2018-02-13 09:10:49.716769+00:00
.TIME_ZONE='Asia/Seoul', USE_TZ=False
2018-02-13 18:10:49.717475
2018-02-13 18:10:49.717486
As you can see here, both print out UTC
time under TIME_ZONE='UTC', USE_TZ=True
condition.
The other settings seem work well.
Or, Did I miss something?