the concept and conversion of time zones
The first thing you need to know about the conversion relationship between time zones is that it's simple: subtract local time from local timezone and the rest is GMT. For example, Beijing time of 18:00 is 18:00+08:00, subtract after is 10:00+00:00, therefore is the GMT 10:00.
You can get local time by adding the GMT to the local timezone. For example, 10:00 of GMT is 10:00+00:00, converted to Pacific Standard Time is added-8 hours, therefore is 02:00-08:00.
and the Pacific Standard Time conversion to the Beijing Times conversion is the same, time zone subtraction can be. For example, the Pacific Standard Time 02:00-08:00, and Beijing time difference-16 hours, so the result is 18:00+08:00.
Processing of the Python time zone
Python does not have a simple way to handle 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 to query a time zone
All time zones for this country can be found by country code.
Copy the Code code as follows:
>>> Import Pytz
>>> pytz.country_timezones (' CN ')
[' Asia/shanghai ', ' asia/harbin ', ' asia/chongqing ', ' Asia/urumqi ', ' Asia/kashgar ']
Pytz creating a Time zone object
Depending on the time zone information obtained above, the specified time zone object can be created. For example, create a Shanghai time zone object:
Copy the Code code as follows:
TZ = Pytz.timezone (' Asia/shanghai ')
Get time for a time zone
Then, when you create the time object, specify the above time zone to get the datetime for the specified time zone:
Copy the Code code as follows:
>>> Import datetime
>>> Datetime.datetime.now (TZ)