(ref_date: str)
| 169 | |
| 170 | |
| 171 | def _ensure_padded_year(ref_date: str) -> str: |
| 172 | # Reference dates without a padded year (e.g. since 1-1-1 or since 2-3-4) |
| 173 | # are ambiguous (is it YMD or DMY?). This can lead to some very odd |
| 174 | # behaviour e.g. pandas (via dateutil) passes '1-1-1 00:00:0.0' as |
| 175 | # '2001-01-01 00:00:00' (because it assumes a) DMY and b) that year 1 is |
| 176 | # shorthand for 2001 (like 02 would be shorthand for year 2002)). |
| 177 | |
| 178 | # Here we ensure that there is always a four-digit year, with the |
| 179 | # assumption being that year comes first if we get something ambiguous. |
| 180 | matches_year = re.match(r".*\d{4}.*", ref_date) |
| 181 | if matches_year: |
| 182 | # all good, return |
| 183 | return ref_date |
| 184 | |
| 185 | # No four-digit strings, assume the first digits are the year and pad |
| 186 | # appropriately |
| 187 | matches_start_digits = re.match(r"(\d+)(.*)", ref_date) |
| 188 | if not matches_start_digits: |
| 189 | raise ValueError(f"invalid reference date for time units: {ref_date}") |
| 190 | ref_year, everything_else = (s for s in matches_start_digits.groups()) |
| 191 | ref_date_padded = f"{int(ref_year):04d}{everything_else}" |
| 192 | |
| 193 | warning_msg = ( |
| 194 | f"Ambiguous reference date string: {ref_date}. The first value is " |
| 195 | "assumed to be the year hence will be padded with zeros to remove " |
| 196 | f"the ambiguity (the padded reference date string is: {ref_date_padded}). " |
| 197 | "To remove this message, remove the ambiguity by padding your reference " |
| 198 | "date strings with zeros." |
| 199 | ) |
| 200 | warnings.warn(warning_msg, SerializationWarning, stacklevel=2) |
| 201 | |
| 202 | return ref_date_padded |
| 203 | |
| 204 | |
| 205 | def _unpack_netcdf_time_units(units: str) -> tuple[str, str]: |
no test coverage detected
searching dependent graphs…