Python and Django are always prone to time zone confusion when coding involves time issues. for a long time, they have been trying to cope with the past. today, they finally figured out why, A knot can also be resolved.
Python time zone problems
Datetime. today ()/datetime. now ()
The two functions obtain the current system time, but the tzinfo in the obtained datetime object is null, even if the time zone is set in the system.
Datetime. utcnow ()
This 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.
Effect of the environment variable TZ on the above functions:
When the environment variable TZ is set in the system, or 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.
Django time zone problems
After understanding the functions in the above python, django's time zone problem 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 code
import pytz....#dt the datetime vardt.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Asia/Shanghai'))