I'm encountering multiple UnknownTimezoneWarning errors when parsing dates with Python's dateutil.parser. The warnings are for various timezone abbreviations like UT, PDT, EDT, EST, PST, CEDT, EET, EEST, CES, and MET. The warning suggests using the tzinfos argument for timezone-aware datetime parsing. How can I effectively address these warnings? Is there a comprehensive tzinfos dictionary available, or should I manually map these timezones? Additionally, why does this issue arise even though I'm using libraries like pendulum, which depends on pytz?
I tried to handle these warnings by creating a tzinfos dictionary, but it's incomplete and doesn't cover all cases. Here's what I've done so far in Python:
import pendulum
# Dictionary to map timezone abbreviations to their UTC offsets
tzinfos = {
"UT": 0, "UTC": 0, "GMT": 0, # Universal Time Coordinated
"EST": -5*3600, "EDT": -4*3600, # Eastern Time
"CST": -6*3600, "CDT": -5*3600, # Central Time
"MST": -7*3600, "MDT": -6*3600, # Mountain Time
"PST": -8*3600, "PDT": -7*3600, # Pacific Time
"HST": -10*3600, "AKST": -9*3600, "AKDT": -8*3600, # Hawaii and Alaska Time
"CEDT": 2*3600, "EET": 2*3600, "EEST": 3*3600, # Central European and Eastern European Time
"CES": 1*3600, "MET": 1*3600 # Central European Summer Time and Middle European Time
}
def convert_dates_to_iso_with_pendulum(data):
for item in data:
# Parse the date using Pendulum with the tzinfos dictionary
parsed_date = pendulum.parse(item["date"], strict=False, tzinfos=tzinfos)
# Convert the date to ISO 8601 format
item["date"] = parsed_date.to_iso8601_string()
return data
This code attempts to map the known timezone abbreviations to their respective UTC offsets. I am looking for advice on improving this approach to cover more cases or handle it more effectively.
is e.g. any of
Getting a comprehensive list of timezones for my usecase of converting date strings with timezone formats to zulu based iso format was quite tricky and is not finished yet.
see also https://wiki.bitplan.com/index.php/Timezones
Below you find the current state of affairs the code is also available as part of the nicegui widgets project for which i am a comitter.
see dateparser.py
dateparser
unit test