【问题标题】:How to get the UTC offset from a timezone string using pytz?如何使用 pytz 从时区字符串中获取 UTC 偏移量?
【发布时间】:2019-04-08 07:02:02
【问题描述】:

我正在尝试使用 Python 库 pytz 从我的时区获取 UTC 偏移量作为字符串。

我是这样定义的:

import pytz
tz = pytz.timezone('Africa/Cairo')

现在,我想从 tz 变量中获取“+02:00”,因为这是非洲/开罗的相应 UTC 偏移量。

我该怎么做?谢谢!

【问题讨论】:

    标签: python datetime pytz


    【解决方案1】:

    您可以使用datetime 获取给定时区的当前日期/时间,然后提取 UTC 偏移量,

    import pytz
    import datetime
    tz = pytz.timezone('Africa/Cairo')
    
    
    print(datetime.datetime.now(tz).utcoffset().total_seconds()/3600)
    # output,
    2.0
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2016-05-07
      • 2013-10-15
      • 1970-01-01
      • 2015-02-14
      • 2015-06-16
      相关资源
      最近更新 更多