【问题标题】:Get the offset for a specific time and timezone in python在python中获取特定时间和时区的偏移量
【发布时间】:2014-12-29 22:45:05
【问题描述】:

我需要一个函数来在特定时刻返回特定时区的偏移量。我在America/Los_Angeles 格式的字符串变量中有时区,我有2014-12-01 格式的日期

偏移量需要对 DST 敏感,这意味着对于洛杉矶,它应该在夏季返回 -07,在冬季返回 -08。

【问题讨论】:

    标签: python timezone-offset


    【解决方案1】:

    您可以使用pytz 将字符串转换为时区对象:

    tz = pytz.timezone('America/Los_Angeles')
    

    使用strptime 将日期转换为datetime 对象:

    dt = datetime.datetime.strptime('2014-12-01', '%Y-%m-%d')
    

    现在可以导出小时偏移量:

    offset = int(tz.utcoffset(dt).total_seconds() / 3600.0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2021-01-20
      • 2021-12-01
      • 2018-03-15
      • 2020-10-21
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多