python pytz模块
Python pytz module allows us to create timezone aware datetime instances.
Python pytz模块允许我们创建时区感知日期时间实例。
Python datetime now() function creates the naive datetime instance from the current local system time. However, this function also takes timezone as an argument that should be the implementation of abstract type tzinfo
.
Python datetime now()函数从当前本地系统时间创建朴素的datetime实例。 但是,此函数还将timezone作为参数,应作为抽象类型tzinfo
的实现。
Python pytz module provides implementations of tzinfo
class that can be used to create timezone aware datetime instances.
Python pytz模块提供tzinfo
类的实现,可用于创建时区感知日期时间实例。
Python pytz module can be installed using PIP command.
可以使用PIP命令安装Python pytz模块。
pip install pytz
There are some attributes in pytz module to help us find the supported timezone strings. Let’s look at them.
pytz模块中有一些属性可帮助我们找到支持的时区字符串。 让我们看看它们。
Returns the list of all the supported timezones by the pytz module.
返回pytz模块支持的所有时区的列表。
import pytz
print('all_timezones =', pytz.all_timezones, '\n')
Output:
输出:
all_timezones = ['Africa/Abidjan', 'Africa/Accra', ... , 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']
The list is very long, the output is just showing some of the values.
该列表很长,输出仅显示一些值。
Returns the set of all the supported timezones.
返回所有支持的时区的集合。
print('all_timezones_set =', pytz.all_timezones_set, '\n')
Output:
输出:
all_timezones_set = LazySet({'America/St_Vincent', 'Asia/Thimphu', 'Etc/GMT+9', ... , 'Europe/Guernsey'})
Note that its a set, so the order of elements is not recorded and output in your system may be in different order.
请注意,它是一个集合,因此不会记录元素的顺序,并且系统中的输出可能会以不同的顺序进行。
Returns the list and set of commonly used timezones.
返回常用时区的列表和集合。
print('common_timezones =', pytz.common_timezones, '\n')
print('common_timezones_set =', pytz.common_timezones_set, '\n')
Output:
输出:
common_timezones = ['Africa/Abidjan', 'Africa/Accra', ... , 'US/Pacific', 'UTC']
common_timezones_set = LazySet({'America/St_Vincent', 'Asia/Thimphu', ... , 'Europe/Guernsey'})
Returns a dict of country ISO Alpha-2 Code as key and country full name as value.
返回国家(地区)ISO Alpha-2代码作为键,国家(地区)全名作为值的字典。
print('country_names =')
for key, val in pytz.country_names.items():
print(key, '=', val, end=',')
print('\n')
print('IN full name =', pytz.country_names['IN'])
Output:
输出:
country_names =
AD = Andorra,AE = United Arab Emirates, ... , ZW = Zimbabwe,
IN full name = India
Returns a dict of country ISO Alpha-2 Code as key and list of supported timezones as value.
返回国家/地区ISO Alpha-2代码的字典作为键,并返回支持的时区列表作为值。
print('country_timezones =')
for key, val in pytz.country_timezones.items():
print(key, '=', val, end=',')
print('\n')
print('Supported timezones by US =', pytz.country_timezones['US'])
Output:
输出:
country_timezones =
AD = ['Europe/Andorra'],AE = ['Asia/Dubai'],...,ZW = ['Africa/Harare'],
Supported timezones by US = ['America/New_York', 'America/Detroit', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Indiana/Indianapolis', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Vevay', 'America/Chicago', 'America/Indiana/Tell_City', 'America/Indiana/Knox', 'America/Menominee', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/North_Dakota/Beulah', 'America/Denver', 'America/Boise', 'America/Phoenix', 'America/Los_Angeles', 'America/Anchorage', 'America/Juneau', 'America/Sitka', 'America/Metlakatla', 'America/Yakutat', 'America/Nome', 'America/Adak', 'Pacific/Honolulu']
Let’s look at some examples of creating datetime instance with timezone information.
让我们看一些使用时区信息创建datetime实例的示例。
# getting utc timezone
utc = pytz.utc
# getting timezone by name
ist = pytz.timezone('Asia/Kolkata')
# getting datetime of specified timezone
print('UTC Time =', datetime.now(tz=utc))
print('IST Time =', datetime.now(tz=ist))
Output:
输出:
UTC Time = 2018-09-20 09:16:46.313898+00:00
IST Time = 2018-09-20 14:46:46.313951+05:30
We can create timezone aware datetime instance from given datetime instance using localize() function. Note that if you are creating current datetime instance then you should use it carefully, otherwise you will get the wrong information if there is a mismatch between local system timezone and pytz timezone provided.
我们可以使用localize()函数从给定的datetime实例创建可识别时区的datetime实例。 请注意,如果要创建当前日期时间实例,则应谨慎使用它,否则,如果提供的本地系统时区和pytz时区不匹配,则会得到错误的信息。
# using localize() function, my system is on IST timezone
local_datetime = ist.localize(datetime.now())
print('IST Current Time =', local_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print('Wrong UTC Current Time =', utc.localize(datetime.now()).strftime('%Y-%m-%d %H:%M:%S %Z%z'))
Output:
输出:
IST Current Time = 2018-09-20 14:53:54 IST+0530
Wrong UTC Current Time = 2018-09-20 14:53:54 UTC+0000
Notice that I am using strftime() function to print timezone information when datetime is formatted to string.
请注意,当datetime格式化为字符串时,我正在使用strftime()函数打印时区信息。
We can use astimezone()
function to get the time into a different timezone. Following code snippet will convert the earlier IST datetime instance to UTC time.
我们可以使用astimezone()
函数将时间转到另一个时区。 以下代码段会将较早的IST datetime实例转换为UTC时间。
# converting IST to UTC
utc_datetime = local_datetime.astimezone(utc)
print('IST Current Time =', local_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print('UTC Time =', utc_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
Output:
输出:
IST Current Time = 2018-09-20 14:56:03 IST+0530
UTC Time = 2018-09-20 09:26:03 UTC+0000
Reference: PYPI Docs
参考: PYPI文件
python pytz模块