@@ -104,7 +104,9 @@ def load_zonefiles(
104
104
def create_package (
105
105
version : str , zonenames : typing .List [str ], zoneinfo_dir : pathlib .Path
106
106
):
107
- """Creates the tzdata package"""
107
+ """Creates the tzdata package."""
108
+ package_version = translate_version (version )
109
+
108
110
# First remove the existing package contents
109
111
target_dir = PKG_BASE / "tzdata"
110
112
if target_dir .exists ():
@@ -119,6 +121,7 @@ def create_package(
119
121
with open (TEMPLATES_DIR / "__init__.py.in" , "r" ) as f_in :
120
122
contents = f_in .read ()
121
123
contents = contents .replace ("%%IANA_VERSION%%" , f'"{ version } "' )
124
+ contents = contents .replace ("%%PACKAGE_VERSION%%" , f'"{ package_version } "' )
122
125
123
126
with open (target_dir / "__init__.py" , "w" ) as f_out :
124
127
f_out .write (contents )
@@ -155,6 +158,42 @@ def find_latest_version() -> str:
155
158
return version
156
159
157
160
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
+
158
197
@click .command ()
159
198
@click .option (
160
199
"--version" , "-v" , default = None , help = "The version of the tzdata file to download"
0 commit comments