python将PST时间转换为UTC isoformat [英] python convert PST time to UTC isoformat

查看:342
本文介绍了python将PST时间转换为UTC isoformat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

ex: I have a date string

2018-02-17 16:15:36.519 PST

如何在UTC中转换为同等格式,如下所示

How do i convert into isoformat in UTC like below

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

EDIT
使用python 2.7。

EDIT using 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进行格式化

then formatting with Z as suggested

推荐答案

要解析带有时区缩写( PST )转换为可识别时区的日期时间对象:

To parse a time string with a timezone abbreviation (PST) into a timezone-aware datetime object:

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时区:

To convert the time to UTC timezone:

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

On Python 2.7 there is no 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 )可能含糊。请参阅在Python中使用带时区缩写名称的日期/时间字符串进行解析?


Note: in the general case the timezone abbreviation (such as PST) may be ambiguous. See Parsing date/time string with timezone abbreviated name in Python?

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

In your specific case, the time string corresponds to unique UTC time:

>>> 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(将不同时区的时间)转换为UTC

这篇关于python将PST时间转换为UTC isoformat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆