Python uses module Pytz to convert time zones

Source: Internet
Author: User
Tags time zones
Preface

Recently encountered a problem: my server and client not in a time zone, server time zone is EDT, that is, the Eastern time zone, client is my own computer, time zone is the Chinese standard Time zone, East eight district. In the test need, I need to server send a time to make the server at this time stamp to perform some actions. This timestamp is usually the current time plus 2 minutes or a few minutes.

Usually the United States East in daylight saving time, and we are 12 hours apart, so directly minus the 12 hours, and then add two minutes, can be sent based on server the timestamp, but only half of the time is daylight savings, so consider or based on the time zone. Baidu a bit, Python has a module pytz is time zone-related, but not the builtin method, so need to install a bit.

1. Install PYTZ,PIP installed Pytz first.

2. Try the water and print out the U.S. time zone:

#-*-coding:utf-8-*-#/usr/bin/env pythonimport pytzprint (pytz.country_timezones (' Us ')) #[u ' America/New_York ', U ' america/detroit ', U ' America/kentucky/louisville ', U ' America/kentucky/monticello ', U ' america/indiana/ Indianapolis ', U ' america/indiana/vincennes ', U ' America/indiana/winamac ', U ' America/indiana/marengo ', U ' america/ Indiana/petersburg ', U ' America/indiana/vevay ', U ' America/chicago ', U ' america/indiana/tell_city ', U ' America/Indiana /knox ', U ' America/menominee ', U ' america/north_dakota/center ', U ' America/north_dakota/new_salem ', U ' America/North_ Dakota/beulah ', U ' america/denver ', U ' america/boise ', U ' America/phoenix ', U ' america/los_angeles ', U ' america/ Anchorage ', U ' America/juneau ', U ' America/sitka ', U ' America/metlakatla ', U ' america/yakutat ', U ' america/nome ', U ' America/adak ', U ' pacific/honolulu ']

It's a lot of places, but since it's east, it's good to choose New York directly.

3. Next, print the current time of the United States East.

#-*-coding:utf-8-*-#/usr/bin/env pythonimport pytzimport Timeimport Datetimetz = Pytz.timezone (' America/New_York ') a = Datetime.datetime.now (TZ). Strftime ("%y-%m-%d%h:%m:%s") print (a)

#2016-08-18 02:26:53

4. Convert the time to seconds, add 120 seconds, and then convert back to standard format:

#-*-coding:utf-8-*-#/usr/bin/env pythonimport pytzimport timeimport datetimeprint (pytz.country_timezones (' Us ')) TZ = Pytz.timezone (' america/new_york ') a = Datetime.datetime.now (TZ). Strftime ("%y-%m-%d%h:%m:%s") print (a) b=time.mktime (Time.strptime (A, '%y-%m-%d%h:%m:%s ')) +int (2) *60print (Time.strftime ("%y-%m-%d%h:%m", Time.localtime (b)))

#2016-08-18 02:28

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.