Django time zone issues

We all know the time zone, the standard time zone is UTC time zone, Django uses UTC time zone by default, so the time we store in the database is UTC time, but when we make a website only for domestic users, or just provide internal platform use, We hope that the time stored in the database is the local time (the time in the East Eighth District), then Django can also fulfill such a demand.

Await time and navie time

What is await time and navie time? It is the two time types in our python

  1. The navie representative doesn’t know which time zone his time is
  2. The await representative knows which time zone his timetable is

Django set the time in East 8th district

We want to change the time zone in django to the time of Dongba District, it is very simple

USE_TZ = False
TIME_ZONE = 'Asia/Shanghai'

In the settings.py file, set USE_TZ to False, and set TIME_ZONE to Asia Shanghai. When we create a time field in the model, the time in the database is stored in the East Eight District, and the type of time will be the navie type , So we can no longer convert the time of the navie type to the type of other time zone, so we generally do not recommend this.

Django set UTC time zone

The UTC time zone is set by default in django, so the time stored in our database is the time in the UTC time zone, which is 0 time zone, which is 8 hours less than what we normally see, but its time is of the await type and can be converted to any time Time zone.

Two time methods are provided in django

  1. django.utils.timezone.now: The current time will be obtained according to whether USE_TZ=True is set in the settings.py file. If it is True, then get a UTC time of the aware type, if it is False, then get a time of the navie type
  2. django.utils.timezone.localtime: According to the TIME_ZONE in setting.py, an aware type of time will be converted to the time in the time zone specified by TIME_ZONE

Leave a Reply