【问题标题】:python convert PST time to UTC isoformatpython将PST时间转换为UTC等格式
【发布时间】:2018-07-28 15:40:11
【问题描述】:

例如: 我有一个日期字符串

2018-02-17 16:15:36.519 PST

如何在 UTC 中转换为 isoformat,如下所示

2018-02-18T00:15:36.519Z

我试过了

from dateutil.parser import parse
d1='2018-02-17 16:15:36.519 PST'
print parse(d1)
it prints like this.  How do i convert it to UTC with Z at the end.
2018-02-17 16:15:36.519000-08:00

编辑 使用python 2.7。

import dateutil
import pytz
from dateutil.parser import parse
d1='2018-02-17 16:15:36.519 PST'
d2=dateutil.parser.parse(d1)
d2.replace(tzinfo=pytz.utc) - d2.utcoffset()
d3=(d2.replace(tzinfo=pytz.utc) - d2.utcoffset()).isoformat()
print d3

然后按照建议使用 Z 进行格式化

【问题讨论】:

标签: python date parsing


【解决方案1】:

要将带有时区缩写 (PST) 的时间字符串解析为可识别时区的日期时间对象:

import dateparser  # pip install dateparser

pst_dt = dateparser.parse('2018-02-17 16:15:36.519 PST')
# -> datetime.datetime(2018, 2, 17, 16, 15, 36, 519000, tzinfo=<StaticTzInfo 'PST'>)

将时间转换为 UTC 时区:

import datetime as DT

utc_dt = pst_dt.astimezone(DT.timezone.utc)
# -> datetime.datetime(2018, 2, 18, 0, 15, 36, 519000, tzinfo=datetime.timezone.utc)

以所需格式打印:

print(utc_dt.isoformat())  # -> 2018-02-18T00:15:36.519000+00:00
print(utc_dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))  # -> 2018-02-18T00:15:36.519000Z

在 Python 2.7 上没有 DT.timezone.utc:

utc_naive = psd_dt.replace(tzinfo=None) - psd_dt.utcoffset()
print utc_naive.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
# -> 2018-02-18T00:15:36.519000Z

注意:在一般情况下,时区缩写(例如PST)可能不明确。见Parsing date/time string with timezone abbreviated name in Python?

在您的具体情况下,时间字符串对应于唯一的 UTC 时间:

>>> from collections import defaultdict
>>> import datetime as DT
>>> import pytz
>>> naive_dt, tzabbr = DT.datetime(2018, 2, 17, 16, 15, 36, 519000), 'PST'
>>> utc_times = defaultdict(list)
>>> for zone in pytz.all_timezones:
...     dt = pytz.timezone(zone).localize(naive_dt, is_dst=None)
...     if dt.tzname() == tzabbr: # same timezone abbreviation
...         utc_times[dt.astimezone(pytz.utc)].append(zone)
>>> for utc_dt, timezones in utc_times.items():
...     print(f'{utc_dt:%c %Z}', *timezones, sep='\n\t')
Sun Feb 18 00:15:36 2018 UTC
        America/Dawson
        America/Ensenada
        America/Los_Angeles
        America/Santa_Isabel
        America/Tijuana
        America/Vancouver
        America/Whitehorse
        Canada/Pacific
        Canada/Yukon
        Mexico/BajaNorte
        PST8PDT
        US/Pacific
        US/Pacific-New

linux convert time(for different timezones) to UTC

【讨论】:

  • 我应该提到我使用的是 python 2.7。我转换如下。 import dateutil import pytz from dateutil.parser import parse d1='2018-02-17 16:15:36.519 PST' d2=dateutil.parser.parse(d1) d2.replace(tzinfo=pytz.utc) - d2.utcoffset( ) d3=(d2.replace(tzinfo=pytz.utc) - d2.utcoffset()).isoformat() 打印 d3
  • @user2406718: dateparser 支持 Python 2.7。我已经用 Python 2.7 变体更新了答案。
【解决方案2】:

这是来自python2.7的演示代码,仅供参考,谢谢!

from datetime import datetime

from pytz import utc, timezone


def get_current_pst_time():
    print('------------(1) Current time to PST time----------------')
    local_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    utc_time = datetime.now(tz=utc).strftime('%Y-%m-%d %H:%M:%S')
    pst_time = datetime.now(tz=utc).astimezone(timezone('US/Pacific')).strftime('%Y-%m-%d %H:%M:%S')
    is_summary_time = bool(datetime.now(tz=utc).astimezone(timezone('US/Pacific')).dst())
    print('is it a summary time? %s.' % is_summary_time)
    print('local time is %s.' % local_time)
    print('utc time is %s.' % utc_time)
    print('pst time is %s.' % pst_time)


def convert_pst_time_to_utc_time(pst_time_str):
    print('------------(2) PST time to UTC time----------------')
    print('pst time is %s.' % pst_time_str)
    temp_time = datetime.strptime(pst_time_str, '%Y-%m-%d %H:%M:%S')
    pacific_timezone = timezone('US/Pacific')
    pst_time = pacific_timezone.localize(temp_time, is_dst=None)
    assert pst_time.tzinfo is not None
    assert pst_time.tzinfo.utcoffset(pst_time) is not None
    is_summary_time = bool(pst_time.dst())
    print('is it a summary time? %s.' % is_summary_time)
    utc_time = pst_time.astimezone(timezone('utc'))
    print('utc time is %s.' % utc_time.strftime('%Y-%m-%d %H:%M:%S'))


def convert_utc_time_to_pst_time(utc_time_str):
    print('------------(3) UTC time to PST time----------------')
    print('utc time is %s.' % utc_time_str)
    temp_time = datetime.strptime(utc_time_str, '%Y-%m-%d %H:%M:%S')
    utc_timezone = timezone('utc')
    utc_time = utc_timezone.localize(temp_time, is_dst=None)
    assert utc_time.tzinfo is not None
    assert utc_time.tzinfo.utcoffset(utc_time) is not None
    pst_time = utc_time.astimezone(timezone('US/Pacific'))
    is_summary_time = bool(pst_time.dst())
    print('is it a summary time? %s.' % is_summary_time)
    print('pst time is %s.' % pst_time.strftime('%Y-%m-%d %H:%M:%S'))


if __name__ == '__main__':
    get_current_pst_time()
    convert_pst_time_to_utc_time('2019-12-03 02:00:00')
    convert_pst_time_to_utc_time('2020-07-03 02:00:00')

    convert_utc_time_to_pst_time('2019-12-03 10:00:00')
    convert_utc_time_to_pst_time('2020-07-03 09:00:00')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-07
    • 2021-09-05
    • 2016-01-13
    • 2014-09-02
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多