The concept and transformation of time zones
The first thing to know about the transition relationship between time zones is that it's simple: subtract local time from the local zone, and the rest is GMT. For example, Beijing time of 18:00 is 18:00+08:00, subtract after is 10:00+00:00, so is Greenwich mean 10:00.
And the GMT plus the local time zone, you can get local times. For example, 10:00 of GMT is 10:00+00:00, converted to Pacific Standard Time is added-8 hours, so it is 02:00-08:00.
and the Pacific Standard Time conversion to the change is the same, time zone subtraction can be. For example, the 02:00-08:00 of the Pacific Standard Time differs from the Beijing time-16 hours, so the result is 18:00+08:00.
Processing of the Python time zone
Discover that Python does not have a simple way of dealing with time zones and does not understand why Python does not provide a time zone module to handle time zone problems. Fortunately we have a third party Pytz module that can help us solve the time zone problem.
Pytz Simple Tutorial
Pytz query for a time zone
You can find all of the country's time zones based on the country code.
Copy Code code as follows:
>>> Import Pytz
>>> pytz.country_timezones (' CN ')
[' Asia/shanghai ', ' asia/harbin ', ' asia/chongqing ', ' Asia/urumqi ', ' Asia/kashgar ']
Pytz Create Time Zone objects
Depending on the time zone information obtained above, you can create the specified time zone object. For example, create a Shanghai time zone object:
Copy Code code as follows:
TZ = Pytz.timezone (' Asia/shanghai ')
Get time in a time zone
You can then specify the date time for the specified time zone by specifying the time zone above when you create it:
Copy Code code as follows:
>>> Import datetime
>>> Datetime.datetime.now (TZ)