簡體   English   中英

python將PST時間轉換為UTC isoformat

[英]python convert PST time to UTC isoformat

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

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 進行格式化

要將帶有時區縮寫 ( 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 )可能不明確。 請參閱在 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 將時間(針對不同時區)轉換為 UTC

這是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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM