目录
pytz
是一个用于处理时区的 Python 库,它使得在 Python 程序中以跨平台、跨时区的方式进行日期和时间操作变得更加简便。pytz
提供了与 IANA(国际互联网号码分配机构)时区数据库兼容的时区信息,确保时区转换的准确性。
1. 安装 pytz
pip install pytz
2. 基本用法
2.1 导入库
import pytz
from datetime import datetime
2.2 获取所有时区
for tz in pytz.all_timezones:
print(tz)
打印结果(示例):
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
...
2.3 获取常用时区
for tz in pytz.common_timezones:
print(tz)
打印结果(示例):
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
...
2.4 创建时区对象
tz = pytz.timezone('America/New_York')
print(tz)
打印结果:
America/New_York
3. 与 datetime 对象的交互
3.1 获取当前时间
# 获取当前时间(UTC)
utc_now = datetime.now(pytz.utc)
print(f"Current UTC time: {utc_now}")
# 获取当前时间(本地时区,不带时区信息)
local_now = datetime.now()
print(f"Current local time: {local_now}")
# 获取当前时间(指定时区)
ny_now = datetime.now(pytz.timezone('America/New_York'))
print(f"Current New York time: {ny_now}")
打印结果(示例):
Current UTC time: 2023-10-05 10:00:00.123456+00:00
Current local time: 2023-10-05 06:00:00.123456
Current New York time: 2023-10-05 06:00:00.123456-04:00
3.2 本地化 datetime 对象
# 创建一个没有时区信息的 datetime 对象
naive_dt = datetime(2023, 10, 5, 12, 0, 0)
# 本地化到 UTC
localized_dt = pytz.utc.localize(naive_dt)
print(f"Localized to UTC: {localized_dt}")
# 本地化到指定时区
ny_tz = pytz.timezone('America/New_York')
localized_dt_ny = ny_tz.localize(naive_dt)
print(f"Localized to New York: {localized_dt_ny}")
打印结果:
Localized to UTC: 2023-10-05 12:00:00+00:00
Localized to New York: 2023-10-05 12:00:00-04:00
4. 时区转换
# 将 UTC 时间转换到 New York 时间
ny_time = localized_dt.astimezone(pytz.timezone('America/New_York'))
print(f"UTC to New York: {ny_time}")
# 将 New York 时间转换到 UTC 时间
utc_time = localized_dt_ny.astimezone(pytz.utc)
print(f"New York to UTC: {utc_time}")
# 将 New York 时间转换到 Asia/Shanghai 时间
shanghai_time = localized_dt_ny.astimezone(pytz.timezone('Asia/Shanghai'))
print(f"New York to Shanghai: {shanghai_time}")
打印结果:
UTC to New York: 2023-10-05 08:00:00-04:00
New York to UTC: 2023-10-05 16:00:00+00:00
New York to Shanghai: 2023-10-06 00:00:00+08:00
5. 处理夏令时(DST)
# 创建一个时间在夏令时开始之前的 datetime 对象
before_dst = datetime(2023, 3, 10, 12, 0, 0)
before_dst_ny = ny_tz.localize(before_dst)
print(f"Before DST in New York: {before_dst_ny}")
# 创建一个时间在夏令时之后的 datetime 对象
after_dst = datetime(2023, 6, 10, 12, 0, 0)
after_dst_ny = ny_tz.localize(after_dst)
print(f"After DST in New York: {after_dst_ny}")
# 转换到其他时区
before_dst_utc = before_dst_ny.astimezone(pytz.utc)
after_dst_utc = after_dst_ny.astimezone(pytz.utc)
print(f"Before DST in UTC: {before_dst_utc}")
print(f"After DST in UTC: {after_dst_utc}")
打印结果:
Before DST in New York: 2023-03-10 12:00:00-05:00
After DST in New York: 2023-06-10 12:00:00-04:00
Before DST in UTC: 2023-03-10 17:00:00+00:00
After DST in UTC: 2023-06-10 16:00:00+00:00
6. 常见操作示例
6.1 获取当前时间并转换到某个时区
# 获取当前 UTC 时间
utc_now = datetime.now(pytz.utc)
print(f"Current UTC time: {utc_now}")
# 转换到 New York 时间
ny_time = utc_now.astimezone(pytz.timezone('America/New_York'))
print(f"Current New York time: {ny_time}")
打印结果(示例):
Current UTC time: 2023-10-05 10:00:00.123456+00:00
Current New York time: 2023-10-05 06:00:00.123456-04:00
6.2 比较两个不同时区的时间
ny_tz = pytz.timezone('America/New_York')
shanghai_tz = pytz.timezone('Asia/Shanghai')
# 本地化两个时间点
ny_time = ny_tz.localize(datetime(2023, 10, 5, 12, 0, 0))
shanghai_time = shanghai_tz.localize(datetime(2023, 10, 6, 0, 0, 0))
# 比较两个时间点
if ny_time < shanghai_time:
print("New York time is earlier than Shanghai time")
else:
print("Shanghai time is earlier or the same as New York time")
打印结果:
New York time is earlier than Shanghai time