Python and Django time zone problems

Source: Internet
Author: User

Bytes. Datetime. today ()/datetime. now () these two functions obtain the current system time, but the tzinfo in the obtained datetime object is empty, even if the time zone is set in the system. The datetime. utcnow () function obtains the current utc time, which is calculated based on the current system time and time zone. For example, the system time is, the time zone is Asia/Shanghai (Beijing time), and the return time of utcnow is. Similarly, the tzinfo in the obtained object is null. The effect of the environment variable TZ on the above functions: when the environment variable TZ is set in the system or the OS is set in python. when environ ['tz '], the time obtained by the above function is the time of TZ's corresponding time zone. In fact, here we can think that TZ does not affect these functions, but the system time, which can be seen from the return results of the date command. Datetime. now () is always the same as the result returned by the date command. The Time Zone of Django understands the functions in the above python, and the time zone of django looks simple. In setting of django, one setting is TIME_ZONE to set the time zone used in the program. According to django's documents, TIME_ZONE is used to change the OS. environ ['tz '], but changes the OS. environ ['tz '] does not change the system environment variable TZ. Therefore, if the TIME_ZONE settings are inconsistent with the system time zone settings, datetime is used in the program. the time obtained by now () is different from the time obtained by the date command. Therefore, TIME_ZONE should be set as the time zone that the program wants to use. For a local program, set TIME_ZONE to the same as the system time zone. For an international application, set TIME_ZONE to UTC, adjust the display time based on the current user's time zone. Manual classmethod datetime. now ([tz]) Return the current local date and time. if optional argument tz is None or not specified, this is like today (), but, if possible, supplies more precision than can be gotten from going through a time. time () timestamp (for example, this may be possible on platforms supplying the C gettimeofday () function ). else tz must be an instance of a class tzinfo subclass, and the current date and time are converted to tz's time zone. in this case the result is equivalent to tz. fromutc (datetime. utcnow (). replace (tzinfo = tz )). see also today (), utcnow (). classmethod datetime. utcnow () Return the current UTC date and time, with tzinfo None. this is like now (), but returns the current UTC date and time, as a naivedatetime object. see also now (). time zone conversion import pytz .... # dt the datetime vardt. replace (tzinfo = pytz. utc ). astimezone (pytz. timezone ('Asia/Shanghai '))

Related Article

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.