这是我测试TIME_ZONE
和`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())
结果
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
奇怪的部分是test_freeze_time1()
,在TIME_ZONE='UTC', USE_TZ=True
下进行测试,我觉得奇怪的是datetime.now()
没有打印UTC
时间,这是如果我删除了@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())
结果
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
正如您在此处所见,两者都会在UTC
条件下打印TIME_ZONE='UTC', USE_TZ=True
时间。
其他设置似乎运作良好。
或者,我错过了什么吗?