如何在Python中正确计算不同时区的当前时间

huangapple go评论67阅读模式
英文:

How to calculate current time in different timezone correctly in Python

问题

我试图计算纽约市的当前时间(东部夏令时,也称为GMT-4时间),给定我目前所在的以色列的当前时间(以色列夏令时,目前是GMT+3时间)。所以现在以色列比纽约市早7小时,但我得到了8小时的差异,纽约市的时间比实际时间早了一小时:

from pytz import timezone
from datetime import datetime
tz1 = timezone('Israel')
dt1 = datetime.now(tz1)
tz2 = timezone('EST')
dt2 = datetime.now(tz2)
print(f'{dt1}  vs  {dt2}')

输出结果: 2023-05-24 17:01:47.167155+03:00 vs 2023-05-24 09:01:47.167219-05:00

有人知道这可能是为什么吗?

英文:

I was trying to calculate the current time in NYC (EST time aka Eastern Daylight time or GMT-4) given current time in Israel (Israel daylight time, currently GMT+3) where I'm currently located. So right now Israel is 7 hrs ahead of NYC, but I get an 8 hr difference, with NYC coming out an hour earlier than it really is:

from pytz import timezone
from datetime import datetime
tz1 = timezone('Israel')
dt1 = datetime.now(tz1)
tz2 = timezone('EST')
dt2 = datetime.now(tz2)
print(f'{dt1}  vs  {dt2} ')

output: 2023-05-24 17:01:47.167155+03:00  vs  2023-05-24 09:01:47.167219-05:00 

Does anyone have an idea why this might be?

答案1

得分: 2

我试图计算纽约市(东部标准时间,即东部夏令时或格林威治时间+4时区)的当前时间。

问题在于你假设“EST”表示“东部夏令时”。实际上不是这样。它总是表示东部标准时间(至少在美国的背景下如此;最好避免完全使用这些缩写),即UTC-5。另外,我强烈建议你永远不要像那样提到“GMT+4”。我知道这是Posix Etc/GMT+4等的含义,但由于与其他几乎所有其他时区都相反,所以非常令人困惑。

纽约时区在东部标准时间(UTC-5)和东部夏令时(UTC-4)之间变化。所以如果这是你的意思,请明确说明:

tz2 = timezone('America/New_York')

我还建议你在源时区中使用Asia/Jerusalem而不是Israel - 使用IANA的标识符。然后,我们有完整的代码:

from pytz import timezone
from datetime import datetime
tz1 = timezone('Asia/Jerusalem')
dt1 = datetime.now(tz1)
tz2 = timezone('America/New_York')
dt2 = datetime.now(tz2)
print(f'{dt1}  vs  {dt2} ')

输出:

2023-05-24 17:43:31.121887+03:00  vs  2023-05-24 10:43:31.122888-04:00
英文:

> I was trying to calculate the current time in NYC (EST time aka Eastern Daylight time or GMT+4)

This is where your problem lies - assuming that "EST" means "Eastern Daylight Time". It doesn't. It always means Eastern Standard Time (at least in the context of the US; ideally it's better to avoid using the abbreviations entirely), which is UTC-5. (As an aside, I'd strongly advise you never to refer to "GMT+4" like that. I know that's what the Posix Etc/GMT+4 etc means, but it's very, very confusing due to being backwards to basically everything else.)

The New York time zone varies between EST and EDT (UTC-5 and UTC-4). So if that's what you mean, say so:

tz2 = timezone('America/New_York')

I'd also suggest using Asia/Jerusalem instead of Israel as your source time zone - use identifiers from IANA. So then we have complete code of:

from pytz import timezone
from datetime import datetime
tz1 = timezone('Asia/Jerusalem')
dt1 = datetime.now(tz1)
tz2 = timezone('America/New_York')
dt2 = datetime.now(tz2)
print(f'{dt1}  vs  {dt2} ')

Output:

2023-05-24 17:43:31.121887+03:00  vs  2023-05-24 10:43:31.122888-04:00

huangapple
  • 本文由 发表于 2023年5月24日 22:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定