Python time zone Setup method and Pytz query time zone tutorial _python

Source: Internet
Author: User
Tags time zones local time

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)

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.