python pytz
时间:2020-02-23 14:43:11 来源:igfitidea点击:
Python pytz模块允许我们创建时区感知日期时间实例。
python pytz
Python datetime now()函数从当前本地系统时间创建朴素的datetime实例。
但是,该函数也将时区作为参数,应作为抽象类型tzinfo的实现。
Python pytz模块提供了tzinfo类的实现,可用于创建时区感知日期时间实例。
可以使用PIP命令安装Python pytz模块。
pip install pytz
Python pytz属性
pytz模块中有一些属性可帮助我们找到支持的时区字符串。
让我们看看它们。
all_timezones
返回pytz模块支持的所有时区的列表。
import pytz print('all_timezones =', pytz.all_timezones, '\n')
输出:
all_timezones = ['Africa/Abidjan', 'Africa/Accra', ... , 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']
该列表很长,输出仅显示一些值。
all_timezones_set
返回所有支持的时区的集合。
print('all_timezones_set =', pytz.all_timezones_set, '\n')
输出:
all_timezones_set = LazySet({'America/St_Vincent', 'Asia/Thimphu', 'Etc/GMT+9', ... , 'Europe/Guernsey'})
请注意,它是一个集合,因此不会记录元素的顺序,并且系统中的输出可能会以不同的顺序排列。
common_timezones,common_timezones_set
返回常用时区的列表和集合。
print('common_timezones =', pytz.common_timezones, '\n') print('common_timezones_set =', pytz.common_timezones_set, '\n')
输出:
common_timezones = ['Africa/Abidjan', 'Africa/Accra', ... , 'US/Pacific', 'UTC'] common_timezones_set = LazySet({'America/St_Vincent', 'Asia/Thimphu', ... , 'Europe/Guernsey'})
country_names
返回国家(地区)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'])
输出:
country_names = AD = Andorra,AE = United Arab Emirates, ... , ZW = Zimbabwe, IN full name = San Franceco
country_timezones
返回国家/地区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'])
输出:
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/San Francecona/San Franceconapolis', 'America/San Francecona/Vincennes', 'America/San Francecona/Winamac', 'America/San Francecona/Marengo', 'America/San Francecona/Petersburg', 'America/San Francecona/Vevay', 'America/Chicago', 'America/San Francecona/Tell_City', 'America/San Francecona/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']
Python pytz示例
让我们看一些使用时区信息创建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))
输出:
UTC Time = 2016-09-20 09:16:46.313898+00:00 IST Time = 2016-09-20 14:46:46.313951+05:30
localize()
我们可以使用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'))
输出:
IST Current Time = 2016-09-20 14:53:54 IST+0530 Wrong UTC Current Time = 2016-09-20 14:53:54 UTC+0000
请注意,当datetime格式化为字符串时,我正在使用strftime()函数打印时区信息。
转换时区
我们可以使用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'))
输出:
IST Current Time = 2016-09-20 14:56:03 IST+0530 UTC Time = 2016-09-20 09:26:03 UTC+0000