博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python pytz模块_python pytz
阅读量:2529 次
发布时间:2019-05-11

本文共 5813 字,大约阅读时间需要 19 分钟。

python pytz模块

Python pytz module allows us to create timezone aware datetime instances.

Python pytz模块允许我们创建时区感知日期时间实例。

python pytz (Python pytz)

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.

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 command.

可以使用命令安装Python pytz模块。

pip install pytz

Python pytz属性 (Python pytz attributes)

There are some attributes in pytz module to help us find the supported timezone strings. Let’s look at them.

pytz模块中有一些属性可帮助我们找到支持的时区字符串。 让我们看看它们。

all_timezones (all_timezones)

Returns the list of all the supported timezones by the pytz module.

返回pytz模块支持的所有时区的列表。

import pytzprint('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.

该列表很长,输出仅显示一些值。

all_timezones_set (all_timezones_set)

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.

请注意,它是一个集合,因此不会记录元素的顺序,并且系统中的输出可能会以不同的顺序进行。

common_timezones,common_timezones_set (common_timezones, common_timezones_set)

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'})

country_names (country_names)

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

country_timezones (country_timezones)

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']

Python pytz示例 (Python pytz example)

Let’s look at some examples of creating datetime instance with timezone information.

让我们看一些使用时区信息创建datetime实例的示例。

# getting utc timezoneutc = pytz.utc# getting timezone by nameist = pytz.timezone('Asia/Kolkata')# getting datetime of specified timezoneprint('UTC Time =', datetime.now(tz=utc))print('IST Time =', datetime.now(tz=ist))

Output:

输出:

UTC Time = 2018-09-20 09:16:46.313898+00:00IST Time = 2018-09-20 14:46:46.313951+05:30

localize() (localize())

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 timezonelocal_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+0530Wrong UTC Current Time = 2018-09-20 14:53:54 UTC+0000

Notice that I am using function to print timezone information when datetime is formatted to string.

请注意,当datetime格式化为字符串时,我正在使用函数打印时区信息。

转换时区 (Converting timezones)

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 UTCutc_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+0530UTC Time = 2018-09-20 09:26:03 UTC+0000
. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

python pytz模块

转载地址:http://yamzd.baihongyu.com/

你可能感兴趣的文章
L1-036 A乘以B
查看>>
RT-Thread信号量使用(动态/静态信号量) 及 信号量的四种使用场合
查看>>
会话 终端
查看>>
copula与概率图模型
查看>>
[翻译]pytest测试框架(一)
查看>>
javascript
查看>>
HTML5简单入门系列(一)
查看>>
rails里routes配置文件里的resources和resource的区别
查看>>
注册 asp.net IIS
查看>>
javascript for循环练习
查看>>
长轮询实现消息推送
查看>>
android 开发入门之 ------ 背景图片
查看>>
Spiral Matrix II
查看>>
C#中Hashtable的用法 转
查看>>
《汇编语言》第二节学习心得
查看>>
【原创】洛谷 LUOGU P3372 【模板】线段树1
查看>>
python笔记8 socket(TCP) subprocess模块 粘包现象 struct模块 基于UDP的套接字协议
查看>>
idea 快捷键 记录
查看>>
iOS:让64位兼容百度地图
查看>>
Docker: Docker容器之间互相通信
查看>>