Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad conversion to UTC #125

Open
sam-s opened this issue Aug 8, 2024 · 0 comments
Open

Bad conversion to UTC #125

sam-s opened this issue Aug 8, 2024 · 0 comments

Comments

@sam-s
Copy link

sam-s commented Aug 8, 2024

Based on my question:

import datetime
from zoneinfo import ZoneInfo
import pandas_market_calendars as mcal

nyse = mcal.get_calendar("NYSE")

def local2utc(date, time):
    return datetime.datetime.combine(date,time).astimezone(datetime.timezone.utc)

for mon in [1,6]:
    date = datetime.datetime(2024,mon,1)
    time0 = nyse.open_time_on(date)
    print(f"date={date}; time={time0}; tz={time0.tzinfo!r}")
    print(f"bad UTC ={local2utc(date,time0)}")
    time1 = time0.replace(tzinfo=ZoneInfo(time0.tzname()))
    print(f"good UTC={local2utc(date,time1)}")

prints

date=2024-01-01 00:00:00; time=09:30:00; tz=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>
bad UTC =2024-01-01 14:26:00+00:00
good UTC=2024-01-01 14:30:00+00:00
date=2024-06-01 00:00:00; time=09:30:00; tz=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>
bad UTC =2024-06-01 14:26:00+00:00
good UTC=2024-06-01 13:30:00+00:00

You can see that local2utc works for stock zoneinfo.ZoneInfo but not for your DstTzInfo on two counts:

  1. it converts to non-round "solar" time
  2. it ignores DST status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant