在哭了很多之后,我可以显示我的国家的正确日期,做这样的事情:
>>> from django.utils.timezone import get_current_timezone
>>> from front.models import Training
>>> tz = get_current_timezone()
>>> stored_date = Training.objects.first().start_date
datetime.datetime(2015, 4, 25, 17, 0, tzinfo=<UTC>)
>>> desired_date = stored_date + tz.utcoffset(stored_date)
datetime.datetime(2015, 4, 25, 14, 0, tzinfo=<UTC>)
该tzinfo
属性显示 UTC,但日期和时间显示正确。
更新 30/10/2015 (Django 1.8)
我今天使用另一种方法,对 django 更友好
>>> from django.utils import timezone
>>> from trainings.models import Training
>>> value = Training.objects.first().date
>>> value
datetime.datetime(2015, 10, 23, 11, 32, 54, 633151, tzinfo=<UTC>)
>>> timezone.localtime(value)
datetime.datetime(2015, 10, 23, 9, 32, 54, 633151, tzinfo=<django.utils.timezone.LocalTimezone object at 0x7fa6129784a8>)