Skip to content

Instantly share code, notes, and snippets.

@intellectronica
Last active June 28, 2024 06:32
Show Gist options
  • Save intellectronica/10dcaf8a44f295888e46a5bdcbb19107 to your computer and use it in GitHub Desktop.
Save intellectronica/10dcaf8a44f295888e46a5bdcbb19107 to your computer and use it in GitHub Desktop.
"Parsing" weird iCal timezones and converting them to standard timezones with GPT-3.5-turbo
# An iCal VTIMEZOME component looks like this:
#
# BEGIN:VTIMEZONE
# TZID:Romance Standard Time
# BEGIN:STANDARD
# DTSTART:16010101T030000
# TZOFFSETFROM:+0200
# TZOFFSETTO:+0100
# RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
# END:STANDARD
# BEGIN:DAYLIGHT
# DTSTART:16010101T020000
# TZOFFSETFROM:+0100
# TZOFFSETTO:+0200
# RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3
# END:DAYLIGHT
# END:VTIMEZONE
vtimezones = {str(vtimezone.get('TZID')): vtimezone.to_ical().decode('utf-8') for vtimezone in cal.walk('VTIMEZONE')}
for vtimezone in cal.walk('VTIMEZONE'):
cal.subcomponents.remove(vtimezone)
class TZMapping(BaseModel):
ical_tz: str
pytz_tz: str
class TZMappings(BaseModel):
mappings: List[TZMapping]
result = ai.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TZMappings,
messages=[
{
"role": "system",
"content": (
"Inspect the provided iCal timezones and provide the corresponding pytz timezones. " +
"Return a mapping from the key of the user-provided iCal timezone to the value of the corresponding pytz timezone.")
},
{
"role": "user",
"content": json.dumps(vtimezones)
}
]
)
tzmap = {tzm.ical_tz: tzm.pytz_tz for tzm in result.mappings}
# tzmap looks like this:
# {'Romance Standard Time': 'Europe/Paris', ...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment