Python time zone time conversion method

Source: Internet
Author: User

I encountered a problem in my recent work: my server and client are not in the same time zone, the server time zone is EDT, that is, the US east time zone, client is my own computer, the China Standard Time Zone (UTC + 8) is the time zone. In the test, I need to send a time to the server so that the server can execute some actions at this timestamp. This timestamp is usually added to the current time by 2 minutes or minutes.

Generally, the difference between US East and US east is 12 hours, so we can directly delete the 12 hours, and then add two more minutes to send the server-based timestamp, but only half of the time is the hour, therefore, it is based on the time zone. Baidu, Python has a module, pytz, which is related to the time zone, but is not the builtin method, so you need to install it.

1. install pytz and pip install pytz first.

2. Try the water and print out the United States time zone:

The code is as follows: Copy code

#-*-Coding: UTF-8 -*-
#/Usr/bin/env python

Import pytz
Print (pytz. country_timezones ('us '))
# [U 'America/New_York ', u 'America/Detroit', u 'America/kentuky/Louisville ', u 'America/kentuky/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'pacpacific/Honolulu ']

This is really a lot of places, but since it is east, just select New York directly.

3. Next, print the current time in the US east.

The code is as follows: Copy code

#-*-Coding: UTF-8 -*-
#/Usr/bin/env python

Import pytz
Import time
Import datetime
Tz = pytz. timezone ('America/New_York ')
A = datetime. datetime. now (tz). strftime ("% Y-% m-% d % H: % M: % S ")
Print ()

#02:26:53

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

The code is as follows: Copy code

#-*-Coding: UTF-8 -*-
#/Usr/bin/env python

Import pytz
Import time
Import datetime

Print (pytz. country_timezones ('us '))
Tz = pytz. timezone ('America/New_York ')
A = datetime. datetime. now (tz). strftime ("% Y-% m-% d % H: % M: % S ")
Print ()
B = time. mktime (time. strptime (a, '% Y-% m-% d % H: % M: % S') + int (2) * 60
Print (time. strftime ("% Y-% m-% d % H: % M", time. localtime (B )))


#


Time format conversion

UTC time format: 2014-09-18T10: 42: 16.126Z
Normal time format: 10:42:16

The code is as follows: Copy code

>>> Import datetime

>>> Utc = "2014-09-18T10: 42: 16.126Z"
>>> Local = "10:42:16"

>>> UTC_FORMAT = "% Y-% m-% dT % H: % M: % S. % fZ"
>>> LOCAL_FORMAT = "% Y-% m-% d % H: % M: % S"

>>> Datetime. datetime. strptime (utc, UTC_FORMAT)
Datetime. datetime (2014, 9, 18, 10, 42, 16,126 000)

>>> Datetime. datetime. strptime (local, LOCAL_FORMAT)
Datetime. datetime (2014, 9, 18, 10, 42, 16)

CODE

Function input/output type: datetime. datetime

The code is as follows: Copy code

Import time
Import datetime


Def utc2local (utc_st ):
"UTC time to local time (+ 8: 00)""
Now_stamp = time. time ()
Local_time = datetime. datetime. fromtimestamp (now_stamp)
Utc_time = datetime. datetime. utcfromtimestamp (now_stamp)
Offset = local_time-utc_time
Local_st = utc_st + offset
Return local_st

Def local2utc (local_st ):
"Local time to UTC time (-)""
Time_struct = time. mktime (local_st.timetuple ())
Utc_st = datetime. datetime. utcfromtimestamp (time_struct)
Return utc_st

Utc_time = datetime. datetime (2014, 9, 18, 10, 42, 16,126 000)

# Utc to local
Local_time = utc2local (utc_time)
Print local_time.strftime ("% Y-% m-% d % H: % M: % S ")
# Output: 18:42:16


# Convert local to utc
Utc_tran = local2utc (local_time)
Print utc_tran.strftime ("% Y-% m-% d % H: % M: % S ")
# Output: 10:42:16

Use third-party plug-ins


Pytz simple tutorial

Pytz queries a time zone
You can use the country code to find all time zones in this country.

The code is as follows: Copy code

 

>>> Import pytz
>>> Pytz. country_timezones ('cn ')
['Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing ', 'Asia/Urumqi', 'Asia/Kashgar']

Pytz creates a time zone object
You can create a specified time zone object based on the time zone information obtained above. For example, create a Shanghai time zone object:

The code is as follows: Copy code

 

Tz = pytz. timezone ('Asia/Shanghai ')

Get the time of a time zone
Then, specify the above time zone when creating the time object to get the date and time of the specified time zone:

The code is as follows: Copy code

 

>>> Import datetime
>>> Datetime. datetime. now (tz)

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.