Need to convert a datetime.tzinfo *region* to a ZoneInfo region

63 views Asked by At

I'm writing a calendar application, and I have been trying to track down why canceled / rescheduled calendar entries keep appearing on my calendar. I traced the problem to a time zone issue with rruleset.

Let's say that I have a recurring weekly event and I'm in the US Eastern time zone. They're all scheduled at noon, but the recurrence overlaps the shift on November 5th from EDT / GMT-4 to EST / GMT-5.

If I do this:

 dtstart = datetime.datetime(2023, 10, 22, 12, 0, 0).astimezone()
 dtend = datetime.datetime(2023, 11, 12, 12, 0, 0).astimezone()
 instances = rrule.rruleset()
 instances.rrule(rrule.rrule(freq=2, dtstart=dtstart, interval=1, wkst=1, until=dtend))
 print(dtstart.tzinfo)
 for i in instances:
     print(i)

...I get four dates, but they're all pegged to EDT, because that's when dtstart begins:

 EDT
 2023-10-22 12:00:00-04:00
 2023-10-29 12:00:00-04:00
 2023-11-05 12:00:00-04:00
 2023-11-12 12:00:00-04:00

But if I do this:

 dtstart = datetime.datetime(2023, 10, 22, 12, 0, 0, tzinfo=zoneinfo.ZoneInfo('US/Eastern'))
 dtend = datetime.datetime(2023, 11, 12, 12, 0, 0, tzinfo=zoneinfo.ZoneInfo('US/Eastern'))
 instances = rrule.rruleset()
 instances.rrule(rrule.rrule(freq=2, dtstart=dtstart, interval=1, wkst=1, until=dtend))
 for i in instances:
     print(i)

...then I get the correctly shifted times:

 2023-10-22 12:00:00-04:00
 2023-10-29 12:00:00-04:00
 2023-11-05 12:00:00-05:00
 2023-11-12 12:00:00-05:00

So it appears that all I need to do is to take a datetime and extract the datetime.tzinfo zone (i.e., US/Eastern) and use it to create a zoneinfo.ZoneInfo object. I'm finding it impossible to do that: ZoneInfo can't figure out the region from the tzname of 'EDT'. Any suggestions?

0

There are 0 answers