【问题标题】:How to get GMT and convert it into PST in Python如何在 Python 中获取 GMT 并将其转换为 PST
【发布时间】:2016-06-06 16:38:55
【问题描述】:

我是 python 新手,但我已经过了一段时间。我玩过 datetime API。我有一个要求,肯定会在某些地方提供帮助,但我无法找到。实际上我正在将UTC时间转换为PST。它对我很好。

tz = timezone("US/Pacific")
utc_date_time = datetime.now(tz=pytz.utc)
opr_date = utc_date_time.astimezone(tz)
print(opr_date)

但我无法获取 GMT 时间并将其转换为 PST。这里有2个问题

  1. 如何获得 GMT 时间
  2. 它将如何转换为 PST

提前感谢您。

【问题讨论】:

  • 您是否了解您的代码已经“获取 GMT 时间并将其转换为 PST”?顺便说一句,你可以get the current time in a given timezone directly(没有从UTC时间显式转换)。
  • 是的,@J.F.Sebastian 你是对的。这只是对不同时区的误解。感谢您的评论。

标签: python-3.x pytz python-datetime


【解决方案1】:

试试这个

获取格林威治标准时间

import os
import time
os.environ["TZ"]="GMT"
print time.strftime("%T %Z", time.localtime(time.time()))

在 PST 中获取它

from datetime import datetime
from pytz import timezone
import pytz
date_format='%m/%d/%Y %H:%M:%S %Z'
date = datetime.now(tz=pytz.utc)
date = date.astimezone(timezone('US/Pacific'))
print 'Local date & time is  :', date.strftime(date_format) 

Local date & time is  : 12/19/2017 01:09:08 PST

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2019-02-18
    • 2017-02-05
    • 2021-03-12
    • 2020-09-07
    • 2011-01-19
    • 2015-08-17
    相关资源
    最近更新 更多