Detailed explanation of Django time and time zone setting

  • 2021-07-24 11:28:31
  • OfStack

When writing to the database to add and subtract time,

django reported an error

TypeError: can't subtract offset-naive and offset-aware datetimes

Modify the setting. py file

Configuration file in Django settings.py There are two configuration parameters related to time and time zone, which are TIME_ZONE And USE_TZ

If USE_TZ is set to True, Django uses the system default time zone, America/Chicago,

At this time TIME_ZONE It doesn't work whether it is set or not.

If USE_TZ is set to False and TIME_ZONE is set to None, Django still uses the default America/Chicago Time.
If TIME_ZONE is set to another time zone, it will be different. If it is an Windows system, the setting of TIME_ZONE is useless, and Django will use the local time. If it is a different system, use the time in this time zone, enter the setting USE_TZ = False, TIME_ZONE = ‘Asia/Shanghai' The UTC time in Shanghai is used

ps: Let's look at the Django time zone problem

After django 1.4, there are two concepts

naive time and active time.

Simply put, naive time is time without time zone, and Active time is time with time zone.

For example, use the datetime.datetime.utcnow()、datetime.datetime.now() The output similar to 2015-05-11 09:10:33. 080451 is the time without time zone (naive time),

And use django.util.timezone.now() The output time similar to 2015-05-11 09:05: 19.936835 +00:00 is the time with time zone (ES80time), where +00:00 indicates the relativity of time zone.

Another concept, UTC time, UTC time represents Greenwich mean time, that is, zero zone time. Beijing time indicates the time of East Zone 8, that is, UTC+8.

Here are some common time zone problems

Question 1: 3 times datetime. datetime. now (), datetime. datetime. utcnow () and django.util.timezone.now() Difference between

datetime. datetime. now (): The output is always local time (naive time) has nothing to do with configuration.

datetime. datetime. utcnow (): If USE_TZ=True is configured in setting, the output time is UTC (ES115time). If USE_TZ=False is configured in setting, the output time is the same as that of True datetime.datetime.now() Exactly the same.

django. util. timezone. now (): If USE_TZ=True is configured in setting, the output is UTC time (active time) TIME_ZONE0 , then the same as datetime.datetime.now() Exactly the same.

Question 2: Does django take less than 8 hours to store to the database than local time?

First of all, one point should be made clear. Before Django 1.4, there was no concept of time zone, and no processing was done for time access and display. The local time was usually stored in the database, which was of course naive time.

Django Stored after 1.4 If USE_TZ=True is set, the time stored to the database is always UTC time. At this time, if USE_TZ=True and TIME_ZONE = 'UTC' are set in settings, the time obtained by datetime. datetime. now () will be stored in the database as UTC time. If you modify the setting to USE_TZ=True And TIME_ZONE = 'Asia/Shanghai' , use datetime.datetime.now() Since the acquired time does not have a time zone, django will regard this time as Asia/Shanghai time, that is, East Zone 8 time, and then django will convert this time into UTC time with time zone and store it in the database, and read it directly according to UTC time, which is the reason why many people on the Internet encounter that the time stored in the database is smaller than the local time for 8 hours.

Question 3: DateTimeField role_cost_history. cost_time received a naive datetime (2015-05-12 19:59:01. 259517) while time zone support is active?

This problem is because if the USE_TZ=True After that, model thinks that DateTimeField uses UTC time (time with time zone), and this problem will be reported if the time obtained with datetime. datetime. now () has no time zone.

Question 4: The output time of django. util. timezone. now () is 8 hours smaller than the local time

As long as it is set USE_TZ=True,django.util.timezone.now() The output is always UTC time, no matter what TIME_ZONE you set. If USE_TZ=False, then django.util.timezone.now() Output equivalent to datetime.datetime.now(), It doesn't matter what TIME_ZONE is set.

Question 5: Template display time

After setting USE_TZ=True, if the TIME_ZONE = 'Asia/Shanghai' Although the UTC time is stored in the database, when the template is displayed, it will be displayed as the local time shown in TIME_ZONE.

Suggestion: In order to unify time 1, when developing django, try to use UTC time, that is, set USE_TZ0 And when getting the time, use the USE_TZ1 Because when the background program uses time, UTC time can be satisfied, and it can also ensure the correct display of certificate template time.

Summarize


Related articles: