【问题标题】:Parse an UTC time, and render it as a local-timezone time解析 UTC 时间,并将其呈现为本地时区时间
【发布时间】:2017-05-27 06:57:33
【问题描述】:

我有一个名为 utctime 的字符串,它是 UTC 时区的日期/时间:

import time
utctime = "2017-01-12 08:38:28"      # UTC
t = time.strptime(utctime, "%Y-%m-%d %H:%M:%S")

如何将此日期/时间呈现为本地服务器时区?这:

time.strftime("%Y-%m-%d %H:%M:%S", t)
# "2017-01-12 08:38:28"

显然不起作用,因为我又得到了 UTC 时间。

是否可以仅使用 timedatetime 模块而不使用其他模块?

PS:我自己是 UTC+1。

【问题讨论】:

    标签: python date datetime time timezone


    【解决方案1】:

    如果您只想坚持使用时间或日期时间,则必须实现自己的时区 (D:)。这是一种使用 pytz 的方法。

    from datetime import datetime
    import pytz    
    utc_time = datetime.utcnow()
    tz = pytz.timezone('US/Pacific')
    
    utc_time =utc_time.replace(tzinfo=pytz.UTC)   
    pst_time=utc_time.astimezone(tz)
    print pst_time
    

    关于图书馆的更多信息是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-31
      • 2019-03-09
      • 2017-04-04
      • 1970-01-01
      • 2018-07-27
      • 2014-10-05
      • 1970-01-01
      • 2018-08-21
      相关资源
      最近更新 更多