@@ -104,7 +104,9 @@ def load_zonefiles(
104104def create_package (
105105 version : str , zonenames : typing .List [str ], zoneinfo_dir : pathlib .Path
106106):
107- """Creates the tzdata package"""
107+ """Creates the tzdata package."""
108+ package_version = translate_version (version )
109+
108110 # First remove the existing package contents
109111 target_dir = PKG_BASE / "tzdata"
110112 if target_dir .exists ():
@@ -119,6 +121,7 @@ def create_package(
119121 with open (TEMPLATES_DIR / "__init__.py.in" , "r" ) as f_in :
120122 contents = f_in .read ()
121123 contents = contents .replace ("%%IANA_VERSION%%" , f'"{ version } "' )
124+ contents = contents .replace ("%%PACKAGE_VERSION%%" , f'"{ package_version } "' )
122125
123126 with open (target_dir / "__init__.py" , "w" ) as f_out :
124127 f_out .write (contents )
@@ -155,6 +158,42 @@ def find_latest_version() -> str:
155158 return version
156159
157160
161+ def translate_version (iana_version : str ) -> str :
162+ """Translates from an IANA version to a PEP 440 version string.
163+
164+ E.g. 2020a -> 2020.1
165+ """
166+
167+ if (
168+ len (iana_version ) < 5
169+ or not iana_version [0 :4 ].isdigit ()
170+ or not iana_version [4 :].isalpha ()
171+ ):
172+ raise ValueError (
173+ "IANA version string must be of the format YYYYx where YYYY represents the "
174+ f"year and x is in [a-z], found: { iana_version } "
175+ )
176+
177+ version_year = iana_version [0 :4 ]
178+ patch_letters = iana_version [4 :]
179+
180+ # From tz-link.html:
181+ #
182+ # Since 1996, each version has been a four-digit year followed by
183+ # lower-case letter (a through z, then za through zz, then zza through zzz,
184+ # and so on).
185+ if len (patch_letters ) > 1 and not all (c == "z" for c in patch_letters [0 :- 1 ]):
186+ raise ValueError (
187+ f"Invalid IANA version number (only the last character may be a letter "
188+ f"other than z), found: { iana_version } "
189+ )
190+
191+ final_patch_number = ord (patch_letters [- 1 ]) - ord ("a" ) + 1
192+ patch_number = (26 * (len (patch_letters ) - 1 )) + final_patch_number
193+
194+ return f"{ version_year } .{ patch_number :d} "
195+
196+
158197@click .command ()
159198@click .option (
160199 "--version" , "-v" , default = None , help = "The version of the tzdata file to download"
0 commit comments