Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python将PST时间转换为UTC isoformat_Python_Date_Parsing - Fatal编程技术网

python将PST时间转换为UTC isoformat

python将PST时间转换为UTC isoformat,python,date,parsing,Python,Date,Parsing,例: 我有一个日期字符串 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.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
编辑 使用Python2.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进行格式化,以将带有时区缩写(
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'>)
要以所需格式打印,请执行以下操作:

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
在Python2.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
)可能不明确。看

在您的特定情况下,时间字符串对应于唯一的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

请参见

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

从日期时间导入日期时间
从pytz导入utc,时区
def get_current_pst_time():
打印('-------------(1)当前时间到PST时间-------------------')
本地时间=datetime.now().strftime(“%Y-%m-%d%H:%m:%S”)
utc\u time=datetime.now(tz=utc).strftime(“%Y-%m-%d%H:%m:%S”)
pst_time=datetime.now(tz=utc).astimezone(时区('US/Pacific')).strftime(“%Y-%m-%d%H:%m:%S”)
is_summary_time=bool(datetime.now(tz=utc).astimezone(时区('US/Pacific')).dst())
打印('是摘要时间吗?%s.%是摘要时间)
打印('本地时间为%s.%local\u时间)
打印('utc时间为%s.%utc\u时间)
打印('pst时间为%s.%pst\u时间)
def将时间转换为utc时间(时间):
打印('----------(2)从太平洋标准时间到UTC时间的时间-------------------')
打印('pst时间为%s.%pst\u时间\u str)
temp_time=datetime.strtime(pst_time_str,%Y-%m-%d%H:%m:%S')
太平洋时区=时区(“美国/太平洋”)
太平洋时间=太平洋时区。本地化(临时时间,is dst=无)
assert pst_time.tzinfo不是无
断言pst_时间.tzinfo.utcoffset(pst_时间)不是无
is_summary_time=bool(pst_time.dst())
打印('是摘要时间吗?%s.%是摘要时间)
utc_time=pst_time.astimezone(时区('utc'))
打印('utc时间为%s.%utc\u时间.strftime('%Y-%m-%d%H:%m:%s'))
def将utc时间转换为utc时间(utc时间):
打印('-------------(3)UTC时间到PST时间-------------------')
打印('utc时间为%s.%utc\u时间\u str)
temp_time=datetime.strtime(utc_time_str,“%Y-%m-%d%H:%m:%S”)
utc\u时区=时区(“utc”)
utc_时间=utc_时区。本地化(临时时间,is_dst=无)
断言utc_time.tzinfo不是无
断言utc_时间.tzinfo.utcoffset(utc_时间)不是无
pst_time=utc_time.astimezone(时区(“美国/太平洋”))
is_summary_time=bool(pst_time.dst())
打印('是摘要时间吗?%s.%是摘要时间)
打印('pst时间为%s.%pst\u时间.strftime('%Y-%m-%d%H:%m:%s'))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
获取当前搜索时间()
将时间转换为utc时间(2019-12-03 02:00:00)
将时间转换为utc时间(2020-07-03 02:00:00)
将utc时间转换为pst时间(“2019-12-03 10:00:00”)
将utc时间转换为pst时间(2020-07-03 09:00:00)

问题可能重复,请不要对此问题提出解决方案。将其作为答案张贴。我应该提到我正在使用Python2.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。我已经用Python2.7变体更新了答案。
>>> 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