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?
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