Python时间操作之pytz模块如何使用

这篇文章主要讲解了“Python时间操作之pytz模块如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python时间操作之pytz模块如何使用”吧!

    1. pytz 模块概述

    什么是 pytz 模块

    pytz 模块是依赖Olson tz数据库导入的,它支持直接使用时区名进行时间计算

    pytz 模块涉及时区,因此其也指定tzinfo信息(详情可见datetime.tzinfo)

    pytz 模块通常与datetime模块结合一起使用,返回具体的时间名

    pytz 模块可以解决夏令时结束时不明确的问题

    重要说明

    pytz 模块支持大多数的时区计算,使用IANA的数据接口,CLDR(Unicode 语言环境)项目提供翻译

    本地还需要按照依赖是时区映射表tzdata数据库(pip install tzdata)

    国家时区映射关系表

    国家/城市代码映射表,pytz库中存储在_CountryTimezoneDict()字典中

    Python时间操作之pytz模块如何使用  python 第1张

    我们可以通过 pytz.country_timezones常量来获取code,timezon

    <pytz._CountryTimezoneDict object at 0x00000256FBE52E30>

    pytz 模块使用方法

    由于pytz是第三方库,因此我们在使用前需要使用pip进行下载其依赖库

    pip install pytz

    代码中使用时,我们需要使用import来进行导入

    # 方式一:导入整个模块
    import pytz
    
    # 方式二:导入具体的库
    from pytz import timezone

    2. pytz 相关方法

    pytz 模块包含国家码查询、时区名等方法

    创建本地化时间:

    方式一:pytz.timezone(tzname).localise()

    tz = pytz.timezone('US/Eastern')
    local_time =tz.localize(datetime.datetime(2022, 6, 13,23, 0, 0))
    print(local_time)

    方式二:local_time.astimezone(tzname)

    ast = local_time.astimezone(tz)

    方式三:tz.normzlize()处理夏令时

    nor = tz.normzlize(datetime.datetime(2022, 6, 13,23, 0, 0))

    时区名获取:

    • 时区名各式化:pytz.timezone(tzname)

    • 获取所有的时区:pytz.country_timezones.values()

    • 获取地区的代码:pytz.country_timezones.keys()

    3. pytz 时区查询

    根据pytz模块相关方法,我们可以写一个函数来实现场景:

    • 输入一个城市:city,如"Simferopol"

    • 输出城市的时区偏离量:如+3

    实现思路,大致如下:

    • 首先调用pytz.country_timezones.values()获取到所有的时区timezones

    • 使用split()将时区的城市名进行分割形成列表city_list

    • 先在city_list.index[city]找到City_index

    • 然后根据City_index在timezones找到时区tzname

    • pytz.timezone(tzname)格式化,算出标准时间

    import pytz
    from datetime import datetime
    
    def timezon_city_gmt(city):
    
        timezons = sum(list(pytz.country_timezones.values()),[])
        cityList = [city.split("/")[1] for city in timezons]
        city_index = cityList.index(city)
        tz = pytz.timezone(timezons[city_index])
        gmt = "GMT" + str(datetime.now().astimezone(tz))[-6:]
    
        return gmt
        
    print(timezon_city_gmt("Simferopol"))
    ---
    GMT+03:00
    ---

    4. pytz 日期计算

    同理,我们日常生活中根据当地时间,算出对方所在时区的当地时间,思路与上述大致一样。

    datetime.strptime()将时间字符串转化成datetime对象

    import pytz
    from datetime import datetime
    
    def update_datetime_tz(olddatetime, city, formate):
        timezons = sum(list(pytz.country_timezones.values()), [])
        cityList = [city.split("/")[1] for city in timezons]
        city_index = cityList.index(city)
        tz = pytz.timezone(timezons[city_index])
        datetime_type = datetime.strptime(olddatetime, formate)
        newdatetime = datetime_type.astimezone(tz)
    
        return newdatetime.strftime(str(formate))
        
        
    print(update_datetime_tz("2022-06-13 12:46:03","Moscow","%Y-%m-%d %H:%M:%S")) 
    ---
    2022-06-13 07:46:03
    ---

    感谢各位的阅读,以上就是“Python时间操作之pytz模块如何使用”的内容了,经过本文的学习后,相信大家对Python时间操作之pytz模块如何使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是蜗牛博客,小编将为大家推送更多相关知识点的文章,欢迎关注!

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    评论

    有免费节点资源,我们会通知你!加入纸飞机订阅群

    ×
    天气预报查看日历分享网页手机扫码留言评论电报频道链接