Time zone issues for Django time

Source: Internet
Author: User
Tags local time

When using the django1.8 version of the project to meet the time of the storage and read inconsistent issues, the Internet to find a lot of posts, but did not speak clearly. This article will be encountered in the project and how to solve the problem as far as possible detailed records, of course, this article refers to a large number of related articles on the Internet.

After django1.4, there are two concepts: naive time and active time. To put it simply, naive time is when you don't have a time zone, and the associated active time is the one with the time zone. For example, a similar 2015-05-11 09:10:33.080451 with Datetime.datetime.utcnow (), Datetime.datetime.now () output is a time without a time zone (naive times), The same time as the 2015-05-11 09:05:19.936835+00:00 with the Django.util.timezone.now () output is the time with the time zone, where +00:00 represents the time zone relativity.

The other one concept is UTC time. Not too much to introduce here, it is necessary to know that UTC time means Greenwich Mean time, that is, 0 zone. The time of the East eight region is utc+8.

Several common time zone issues are listed below, and are provided for related reasons, if any, please note.

Question 1:3 time difference between Datetime.datetime.now (), Datetime.datetime.utcnow () and Django.util.timezone.now ()

Datetime.datetime.now (): The output is always local (naive time) with no relationship to the configuration. Datetime.datetime.utcnow (): If Use_tz=true is configured in setting, the output is in UTC (Naive time), if setting is configured in Use_tz=false, The output time is exactly the same as Datetime.datetime.now (). Django.util.timezone.now (): If Use_tz=true is configured in setting, the output is in UTC (Active time), and if Use_tz=false is configured, is exactly the same as Datetime.datetime.now ().

Question two: Does Django store to the database 8 hours less time than local time?

The first thing to be clear, Django1.4 version before the time zone is no concept, the time to access, display do not do any processing, the database is usually stored in local time, of course, is naive times.

Django Stores after version 1.4 if Use_tz=true is set, the time that is stored in the database is always UTC time. At this point, if use_tz=true and Time_zone = ' UTC ' are set inside the settings, the time Django takes with Datetime.datetime.now () will be stored in the database as UTC time. If the modification is set to use_tz=true and Time_zone = ' Asia/shanghai ', the time taken with Datetime.datetime.now () is not with the timezone, and Django will take this time as Asia/shanghai time. , that is, East eight, and Django will turn this time into a UTC time to store it in the database, and read it directly in UTC, which is why many people on the web are experiencing less than 8 hours of local time stored in the database.

Issue three: Datetimefield Role_cost_history.cost_time received a naive datetime (2015-05-12 19:59:01.259517) while time zone supp ORT is active?

The problem is that if use_tz=true is set, the model thinks that Datetimefield uses UTC time (time with time zone), then the time taken with Datetime.datetime.now () will be reported with no time zone.

Issue four: Django.util.timezone.now () output time is 8 hours smaller than local time

As long as the Use_tz=true,django.util.timezone.now () output is always UTC time, no matter what time_zone you set. If Use_tz=false, the Django.util.timezone.now () output is equivalent to Datetime.datetime.now (), and no matter what time_zone is set.

Question five: Template display time

When Use_tz=true is set, Time_zone = ' Asia/shanghai ' is set, although the database is stored in UTC time, but when the template is displayed, it is displayed in the local time shown in Time_zone.

Recommendation: In order to unify time, when Django develops, try to use UTC time, that is, set use_tz=true,time_zone = ' Asia/shanghai ', and use Django.util.timezone.now () when acquiring time. Because the background program uses time to be able to meet the UTC time, it can also guarantee the correct display of the certificate template time.

Time zone issues for Django time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.