Skip to content

Bad conversion to UTC #125

Description

@sam-s

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions