这是我测试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
。
其他设置似乎运行良好。
或者,我错过了什么吗?