Python time zone setting method with Pytz query time zone tutorial

Source: Internet
Author: User
Tags gmt 10 time zones local time
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)
  • 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.