Python皮茨
Pytz 将 Olson tz 数据库引入Python ,因此支持几乎所有时区。该模块提供日期时间转换功能,并帮助用户为国际客户群提供服务。它可以在我们的Python应用程序中进行时区计算,还允许我们创建时区感知日期时间实例。
安装
Python pytz 模块可以通过给定的方式安装。
- 使用命令行:
pip install pytz
- 使用 tarball,以管理用户身份运行以下命令:
python setup.py install
- 使用 setuptools,将从Python包索引中为您下载最新版本:
easy_install --upgrade pytz
转换时区
通过使用 astimezone()函数,我们可以将时间转换为不同的时区。
Syntax: astimezone(t)
Parameter: t is time to be converted
Return: Converted timezone
例子:
Python3
from datetime import datetime
from pytz import timezone
format = "%Y-%m-%d %H:%M:%S %Z%z"
# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print(now_utc.strftime(format))
# Convert to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print(now_asia.strftime(format))
Python3
import pytz
print('the supported timezones by the pytz module:',
pytz.all_timezones, '\n')
Python3
import pytz
print('all the supported timezones set:',
pytz.all_timezones_set, '\n')
Python3
import pytz
print('Commonly used time-zones:',
pytz.common_timezones, '\n')
print('Commonly used time-zones-set:',
pytz.common_timezones_set, '\n')
Python3
import pytz
print('country_names =')
for key, val in pytz.country_names.items():
print(key, '=', val, end=',')
print('\n')
print('equivalent country name to the input code: =',
pytz.country_names['IN'])
Python3
import pytz
print('country_timezones =')
for key, val in pytz.country_timezones.items():
print(key, '=', val, end=',')
print('\n')
print('Time-zones supported by Antartica =', pytz.country_timezones['AQ'])
Python3
# import the modules
import pytz
import datetime
from datetime import datetime
# getting utc timezone
utc = pytz.utc
# getting timezone by name
kiev_tz = pytz.timezone('Europe/Kiev')
print('UTC Time =', datetime.now(tz=utc))
print('IST Time =', datetime.now(tz=kiev_tz))
Python3
# import the modules
import pytz
import datetime
d = datetime.datetime(1984, 1, 10, 23, 30)
# strftime method allows you to print a string
# formatted using a series of formatting directives
d1 = d.strftime("%B %d, %Y")
# isoformat method used for quickly generating
# an ISO 8601 formatted date/time
d2 = d.isoformat()
print(d1)
print(d2)
Python3
import pytz
import datetime
from datetime import datetime
# using localize() function, my system is on IST timezone
ist = pytz.timezone('Asia/Kolkata')
utc = pytz.utc
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'))
Python3
from datetime import datetime
from pytz import timezone
# Set the time to noon on 2019-08-19
naive = datetime(2019, 8, 19, 12, 0)
# Let's treat this time as being in the UTC timezone
aware = timezone('UTC').localize(naive)
print(aware)
输出
2020-12-30 04:38:16 UTC+0000
2020-12-30 10:08:16 IST+0530
Python pytz 属性
pytz 模块中有一些属性可以帮助我们找到支持的时区字符串。这些属性将有助于更好地理解这个模块。
- 所有时区
它使用 pytz.all_timezones 返回所有可用时区的列表:
蟒蛇3
import pytz
print('the supported timezones by the pytz module:',
pytz.all_timezones, '\n')
输出:
上面的输出显示了一些值,因为列表非常大。
- all_timezones_set
它使用 pytz.all_timezones_set 返回所有可用时区的集合:
蟒蛇3
import pytz
print('all the supported timezones set:',
pytz.all_timezones_set, '\n')
输出
输出顺序在您的系统中会有所不同,因为它是一个集合。
- Common_timezones, Common_timezones_set
它使用 pytz.common_timezones、pytz.common_timezones_set 返回常用时区的列表和集合。
蟒蛇3
import pytz
print('Commonly used time-zones:',
pytz.common_timezones, '\n')
print('Commonly used time-zones-set:',
pytz.common_timezones_set, '\n')
输出
- 国家名称
它以键值对的形式返回国家 ISO Alpha-2 代码和国家名称的字典。
蟒蛇3
import pytz
print('country_names =')
for key, val in pytz.country_names.items():
print(key, '=', val, end=',')
print('\n')
print('equivalent country name to the input code: =',
pytz.country_names['IN'])
输出
country_names =AD = Andorra,AE = United Arab Emirates,TD = Chad,….,ZA = South Africa,ZM = Zambia,ZW = Zimbabwe,
equivalent country name to the input code: India
- country_timezones
它返回一个国家 ISO Alpha-2 代码字典作为键和支持的特定输入键(国家代码)的时区列表作为值
蟒蛇3
import pytz
print('country_timezones =')
for key, val in pytz.country_timezones.items():
print(key, '=', val, end=',')
print('\n')
print('Time-zones supported by Antartica =', pytz.country_timezones['AQ'])
输出
country_timezones =
AD = [‘Europe/Andorra’],AE = [‘Asia/Dubai’],AF = [‘Asia/Kabul’],…..,ZM = [‘Africa/Lusaka’],ZW = [‘Africa/Harare’],
Time-zones supported by Antartica = [‘Antarctica/McMurdo’, ‘Antarctica/Casey’, ‘Antarctica/Davis’, ‘Antarctica/DumontDUrville’, ‘Antarctica/Mawson’, ‘Antarctica/Palmer’, ‘Antarctica/Rothera’, ‘Antarctica/Syowa’, ‘Antarctica/Troll’, ‘Antarctica/Vostok’]
Python pytz 示例
下面给出了一些示例,说明如何使用此模块。
示例 1:使用时区信息创建日期时间实例。
蟒蛇3
# import the modules
import pytz
import datetime
from datetime import datetime
# getting utc timezone
utc = pytz.utc
# getting timezone by name
kiev_tz = pytz.timezone('Europe/Kiev')
print('UTC Time =', datetime.now(tz=utc))
print('IST Time =', datetime.now(tz=kiev_tz))
输出
UTC Time = 2020-12-15 08:23:17.063960+00:00
IST Time = 2020-12-15 10:23:17.063988+02:00
示例 2:
蟒蛇3
# import the modules
import pytz
import datetime
d = datetime.datetime(1984, 1, 10, 23, 30)
# strftime method allows you to print a string
# formatted using a series of formatting directives
d1 = d.strftime("%B %d, %Y")
# isoformat method used for quickly generating
# an ISO 8601 formatted date/time
d2 = d.isoformat()
print(d1)
print(d2)
输出
January 10, 1984
1984-01-10T23:30:00
本地化()
localize() 是用于创建具有初始固定日期时间值的日期时间感知对象的正确函数。生成的日期时间感知对象将具有原始日期时间值。这个函数是由Python库提供的。 pytz.localize() 可用于了解幼稚的时区。当前端客户端将日期时间发送到后端以被视为特定时区(通常是 UTC)时,它很有用。
例子 :
蟒蛇3
import pytz
import datetime
from datetime import datetime
# using localize() function, my system is on IST timezone
ist = pytz.timezone('Asia/Kolkata')
utc = pytz.utc
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 = 2020-12-15 08:49:56 IST+0530
Wrong UTC Current Time = 2020-12-15 08:49:56 UTC+0000
例子
蟒蛇3
from datetime import datetime
from pytz import timezone
# Set the time to noon on 2019-08-19
naive = datetime(2019, 8, 19, 12, 0)
# Let's treat this time as being in the UTC timezone
aware = timezone('UTC').localize(naive)
print(aware)
输出
2019-08-19 12:00:00+00:00