【问题标题】:GMT Timezone conversion ADDS difference in time rather than subtracts [duplicate]GMT 时区转换添加时间差异而不是减去 [重复]
【发布时间】:2020-12-14 02:35:25
【问题描述】:

我正在尝试使用此函数转换时间:

import pytz
from datetime import datetime

def ConvertTimezone(FromZone, timestring, ToZone):
    print("Start of ConvertTimezone from " + str(FromZone) + " " + timestring + " to " + str(ToZone))
    TFORMAT = "%Y %m %d %H:%M"
    ftz = pytz.timezone(FromZone)
    ttz = pytz.timezone(ToZone)
    dt_str = datetime.strptime(timestring, TFORMAT)
    dt_obj_ftz = ftz.localize(dt_str) #localising accounts for daylight savings          
    totime = dt_obj_ftz.astimezone(ttz)
    return totime.strftime(TFORMAT)

print(ConvertTimezone("Etc/GMT+10", "2020 08 30 12:00", "Etc/GMT-12"))

我希望输出是

"2020 08 29 14:00" # the day before

但是我得到了这个,好像它增加了时区差异(22 小时)而不是减去它:

"2020 08 31 10:00" # the day after

用户应该能够使用 pytz.all_timezones 中的任何时区。 我应该怎么做才能让它工作?

【问题讨论】:

  • 注意,您应该尽可能选择基于地区的时区(例如Australia/Melbourne)。 Etc/GMT±X 区域通常用于海上船舶。

标签: python-3.x timezone dst pytz gmt


【解决方案1】:

在这里找到结果: Printing datetime as pytz.timezone("Etc/GMT-5") yields incorrect result 显然,由于某种原因,pytz 的 Etc/GMT 时区的优缺点是相反的。

【讨论】:

  • 嗨。我已将您的问题作为您提供的链接的副本和另一个问题关闭。 (这个问题很好,但之前已经回答过。)您应该删除此答案,因为不鼓励仅链接的答案。谢谢。
猜你喜欢
  • 2013-08-13
  • 1970-01-01
  • 2015-11-11
  • 2021-01-01
  • 1970-01-01
  • 2020-08-30
  • 2021-01-10
  • 2011-12-24
  • 1970-01-01
相关资源
最近更新 更多