将日期时间从欧盟时区转换为太平洋标准时间



我有一个欧盟时区的日期时间列表:

[u'2014-11-01T09:00:00+01:00', u'2014-11-02T00:00:00+01:00', u'2014-11-03T00:00:00+01:00', u'2014-11-04T00:00:00+01:00', u'2014-11-05T00:00:00+01:00', u'2014-11-06T00:00:00+01:00', u'2014-11-07T00:00:00+01:00', u'2014-11-08T00:00:00+01:00', u'2014-11-09T00:00:00+01:00', u'2014-11-10T00:00:00+01:00', u'2014-11-11T00:00:00+01:00', u'2014-11-12T00:00:00+01:00', u'2014-11-13T00:00:00+01:00', u'2014-11-14T00:00:00+01:00', u'2014-11-15T00:00:00+01:00', u'2014-11-16T00:00:00+01:00', u'2014-11-17T00:00:00+01:00', u'2014-11-18T00:00:00+01:00', u'2014-11-19T00:00:00+01:00', u'2014-11-20T00:00:00+01:00', u'2014-11-21T00:00:00+01:00', u'2014-11-22T00:00:00+01:00', u'2014-11-23T00:00:00+01:00', u'2014-11-24T00:00:00+01:00', u'2014-11-25T00:00:00+01:00', u'2014-11-26T00:00:00+01:00', u'2014-11-27T00:00:00+01:00', u'2014-11-28T00:00:00+01:00', u'2014-11-29T00:00:00+01:00', u'2014-11-30T00:00:00+01:00', u'2014-12-01T00:00:00+01:00']

如何将它们中的每一个转换为 PST 时区?

这应该可以做到:

from pytz import timezone
import pytz
from dateutil.parser import parse
l = [u'2014-11-01T09:00:00+01:00', u'2014-11-02T00:00:00+01:00', u'2014-11-03T00:00:00+01:00', u'2014-11-04T00:00:00+01:00', u'2014-11-05T00:00:00+01:00', u'2014-11-06T00:00:00+01:00', u'2014-11-07T00:00:00+01:00', u'2014-11-08T00:00:00+01:00', u'2014-11-09T00:00:00+01:00', u'2014-11-10T00:00:00+01:00', u'2014-11-11T00:00:00+01:00', u'2014-11-12T00:00:00+01:00', u'2014-11-13T00:00:00+01:00', u'2014-11-14T00:00:00+01:00', u'2014-11-15T00:00:00+01:00', u'2014-11-16T00:00:00+01:00', u'2014-11-17T00:00:00+01:00', u'2014-11-18T00:00:00+01:00', u'2014-11-19T00:00:00+01:00', u'2014-11-20T00:00:00+01:00', u'2014-11-21T00:00:00+01:00', u'2014-11-22T00:00:00+01:00', u'2014-11-23T00:00:00+01:00', u'2014-11-24T00:00:00+01:00', u'2014-11-25T00:00:00+01:00', u'2014-11-26T00:00:00+01:00', u'2014-11-27T00:00:00+01:00', u'2014-11-28T00:00:00+01:00', u'2014-11-29T00:00:00+01:00', u'2014-11-30T00:00:00+01:00', u'2014-12-01T00:00:00+01:00']
amsterdam = timezone('Europe/Amsterdam')
pst = timezone('US/Pacific')
[parse(d).replace(tzinfo=amsterdam).astimezone(pst) for d in l]

有两个独立的任务:

  1. 将 RFC 3339 日期/时间格式解析为可感知的日期时间对象

    >>> from dateutil.parser import parse
    >>> aware_dt = parse('2014-11-01T09:00:00+01:00')
    >>> aware_dt
    datetime.datetime(2014, 11, 1, 9, 0, tzinfo=tzoffset(None, 3600))
    
  2. 将其转换为America/Los_Angeles时区

    >>> import pytz
    >>> tz = pytz.timezone('America/Los_Angeles')
    >>> tz.normalize(aware_dt.astimezone(tz))
    datetime.datetime(2014, 11, 1, 1, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
    

相关内容

  • 没有找到相关文章

最新更新