【问题标题】:Add time zone offset (in ISO 8601 format) to naive datetime将时区偏移量(ISO 8601 格式)添加到原始日期时间
【发布时间】:2014-04-15 20:24:51
【问题描述】:

我需要将一系列天真的日期时间转换为它们的本地 tz。本地 tz 以 ISO8601 格式单独存储(例如 PST 的“-0800”)。

我尝试用新的日期时间替换日期时间,添加偏移量:

>>>utc_time 
datetime.datetime(2014, 1, 24, 0, 32, 30, 998654)
>>>tz_offset
u'-0800'
>>>local_time = utc_time.replace(tzinfo=tz_offset)
*** TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'unicode'

并尝试使用pytz来localize(),这需要先调用timezone():

>>>timezone(tz_offset)
*** UnknownTimeZoneError: '-0800'

*此步骤的文档:http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

对使这些偏移有效的任何建议?

*类似的问题here,但我认为使用了不同的格式。

【问题讨论】:

    标签: python datetime pytz


    【解决方案1】:

    同一时区在不同日期可能有不同的 UTC 偏移量。使用时区名称而不是字符串 utc 偏移量:

    import datetime
    import pytz # $ pip install pytz
    
    utc_time = datetime.datetime(2014, 1, 24, 0, 32, 30, 998654)
    utc_dt = utc_time.replace(tzinfo=pytz.utc) # make it timezone aware
    pc_dt = utc_dt.astimezone(pytz.timezone('America/Los_Angeles')) # convert to PST
    
    print(pc_dt.strftime('%Y-%m-%d %H:%M:%S.%f %Z%z'))
    # -> 2014-01-23 16:32:30.998654 PST-0800
    

    【讨论】:

      【解决方案2】:

      正如错误消息所说,您需要一个tzinfo 子类(即tzinfo object),pytz.timezone 从时区字符串返回,但它不理解您提供的偏移格式。

      Another relevant thread to your problem,链接到这个google app engine application,它还提供了一些源代码。如果您愿意,这是一个简单而简单的示例。

      class NaiveTZInfo(datetime.tzinfo):
      
          def __init__(self, hours):
              self.hours = hours
      
          def utcoffset(self, dt):
              return datetime.timedelta(hours=self.hours)
      
          def dst(self, dt):
              return datetime.timedelta(0)
      
          def tzname(self, dt):
              return '+%02d' % self.hours
      

      要处理您的偏移格式,您必须为您提供的格式编写自己的解析逻辑。

      >>> t = NaiveTZInfo(-5)
      >>> u = datetime.datetime(2014, 1, 24, 0, 32, 30, 998654)
      >>> v = u.replace(tzinfo=t)
      >>> str(v)
      '2014-01-24 00:32:30.998654-05:00'
      

      【讨论】:

        猜你喜欢
        • 2016-06-21
        • 2019-09-07
        • 2017-03-31
        • 2013-11-27
        • 2013-06-29
        相关资源
        最近更新 更多