| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
"""This provides data to drive ISO 8601 parsing functionality.""" |
| 20 |
|
| 21 |
import re |
| 22 |
from . import timezone |
| 23 |
from metomi.isodatetime.exceptions import StrftimeSyntaxError |
| 24 |
|
| 25 |
DATE_EXPRESSIONS = { |
| 26 |
"basic": { |
| 27 |
"complete": """ |
| 28 |
CCYYMMDD |
| 29 |
+XCCYYMMDD # '+' stands for either '+' or '-' |
| 30 |
CCYYDDD |
| 31 |
+XCCYYDDD |
| 32 |
CCYYWwwD |
| 33 |
+XCCYYWwwD""", |
| 34 |
"reduced": """ |
| 35 |
CCYY-MM # Deviation? Not clear if "basic" or "extended" in standard. |
| 36 |
CCYY |
| 37 |
CC |
| 38 |
+XCCYY-MM # Deviation? Not clear if "basic" or "extended" in standard. |
| 39 |
+XCCYY |
| 40 |
+XCC |
| 41 |
CCYYWww |
| 42 |
+XCCYYWww""", |
| 43 |
"truncated": """ |
| 44 |
-YYMM |
| 45 |
-YY |
| 46 |
--MMDD |
| 47 |
--MM |
| 48 |
---DD |
| 49 |
YYMMDD |
| 50 |
YYDDD |
| 51 |
-DDD |
| 52 |
YYWwwD |
| 53 |
YYWww |
| 54 |
-zWwwD |
| 55 |
-zWww |
| 56 |
-WwwD |
| 57 |
-Www |
| 58 |
-W-D |
| 59 |
""" |
| 60 |
}, |
| 61 |
"extended": { |
| 62 |
"complete": """ |
| 63 |
CCYY-MM-DD |
| 64 |
+XCCYY-MM-DD |
| 65 |
CCYY-DDD |
| 66 |
+XCCYY-DDD |
| 67 |
CCYY-Www-D |
| 68 |
+XCCYY-Www-D""", |
| 69 |
"reduced": """ |
| 70 |
CCYY-MM |
| 71 |
+XCCYY-MM |
| 72 |
CCYY-Www |
| 73 |
+XCCYY-Www""", |
| 74 |
"truncated": """ |
| 75 |
-YY-MM |
| 76 |
--MM-DD |
| 77 |
YY-MM-DD |
| 78 |
YY-DDD |
| 79 |
-DDD # Deviation from standard ? |
| 80 |
YY-Www-D |
| 81 |
YY-Www |
| 82 |
-z-WwwD |
| 83 |
-z-Www |
| 84 |
-Www-D |
| 85 |
""" |
| 86 |
} |
| 87 |
} |
| 88 |
TIME_EXPRESSIONS = { |
| 89 |
"basic": { |
| 90 |
"complete": """ |
| 91 |
# No Time Zone |
| 92 |
hhmmss |
| 93 |
|
| 94 |
# No Time Zone - decimals |
| 95 |
hhmmss,tt |
| 96 |
hhmm,nn |
| 97 |
hh,ii |
| 98 |
hhmmss.tt |
| 99 |
hhmm.nn |
| 100 |
hh.ii |
| 101 |
""", |
| 102 |
"reduced": """ |
| 103 |
# No Time Zone |
| 104 |
hhmm |
| 105 |
hh |
| 106 |
|
| 107 |
# No Time Zone - decimals |
| 108 |
""", |
| 109 |
"truncated": """ |
| 110 |
# No Time Zone |
| 111 |
-mmss |
| 112 |
-mm |
| 113 |
--ss |
| 114 |
|
| 115 |
# No Time Zone - decimals |
| 116 |
-mmss,tt |
| 117 |
-mm,nn |
| 118 |
--ss,tt |
| 119 |
-mmss.tt |
| 120 |
-mm.nn |
| 121 |
--ss.tt |
| 122 |
""" |
| 123 |
}, |
| 124 |
"extended": { |
| 125 |
"complete": """ |
| 126 |
# No Time Zone |
| 127 |
hh:mm:ss |
| 128 |
|
| 129 |
# No Time Zone - decimals |
| 130 |
hh:mm:ss,tt |
| 131 |
hh:mm,nn |
| 132 |
hh,ii # Deviation? Not allowed in standard ? |
| 133 |
hh:mm:ss.tt |
| 134 |
hh:mm.nn |
| 135 |
hh.ii # Deviation? Not allowed in standard ? |
| 136 |
""", |
| 137 |
"reduced": """ |
| 138 |
# No Time Zone |
| 139 |
hh:mm |
| 140 |
hh # Deviation? Not allowed in standard ? |
| 141 |
""", |
| 142 |
"truncated": """ |
| 143 |
# No Time Zone |
| 144 |
-mm:ss |
| 145 |
-mm # Deviation? Not allowed in standard ? |
| 146 |
--ss # Deviation? Not allowed in standard ? |
| 147 |
|
| 148 |
# No Time Zone - decimals |
| 149 |
-mm:ss,tt |
| 150 |
-mm,nn # Deviation? Not allowed in standard ? |
| 151 |
--ss,tt # Deviation? Not allowed in standard ? |
| 152 |
-mm:ss.tt |
| 153 |
-mm.nn # Deviation? Not allowed in standard ? |
| 154 |
--ss.tt # Deviation? Not allowed in standard ? |
| 155 |
""" |
| 156 |
} |
| 157 |
} |
| 158 |
TIME_ZONE_EXPRESSIONS = { |
| 159 |
"basic": """ |
| 160 |
Z |
| 161 |
+hh |
| 162 |
+hhmm |
| 163 |
""", |
| 164 |
"extended": """ |
| 165 |
Z |
| 166 |
+hh # Deviation? Not allowed in standard? |
| 167 |
+hh:mm |
| 168 |
""" |
| 169 |
} |
| 170 |
TIME_DESIGNATOR = "T" |
| 171 |
_DATE_TRANSLATE_INFO = [ |
| 172 |
(r"\+(?=X)", "(?P<year_sign>[-+])", |
| 173 |
"%(year_sign)s", "year_sign"), |
| 174 |
(r"CC", r"(?P<century>[0-9][0-9])", |
| 175 |
"%(century)02d", "century"), |
| 176 |
(r"YY", r"(?P<year_of_century>[0-9][0-9])", |
| 177 |
"%(year_of_century)02d", "year_of_century"), |
| 178 |
(r"MM", r"(?P<month_of_year>[0-9][0-9])", |
| 179 |
"%(month_of_year)02d", "month_of_year"), |
| 180 |
(r"DDD", r"(?P<day_of_year>[0-9][0-9][0-9])", |
| 181 |
"%(day_of_year)03d", "day_of_year"), |
| 182 |
(r"DD", r"(?P<day_of_month>[0-9][0-9])", |
| 183 |
"%(day_of_month)02d", "day_of_month"), |
| 184 |
(r"Www", r"W(?P<week_of_year>[0-9][0-9])", |
| 185 |
"W%(week_of_year)02d", "week_of_year"), |
| 186 |
(r"D", r"(?P<day_of_week>[0-9])", |
| 187 |
"%(day_of_week)01d", "day_of_week"), |
| 188 |
(r"z", r"(?P<year_of_decade>[0-9])", |
| 189 |
"%(year_of_decade)01d", "year_of_decade"), |
| 190 |
(r"^---", r"(?P<truncated>---)", |
| 191 |
"---", None), |
| 192 |
(r"^--", r"(?P<truncated>--)", |
| 193 |
"--", None), |
| 194 |
(r"^-", r"(?P<truncated>-)", |
| 195 |
"-", None) |
| 196 |
] |
| 197 |
_TIME_TRANSLATE_INFO = [ |
| 198 |
(r"(?<=^hh)mm", r"(?P<minute_of_hour>[0-9][0-9])", |
| 199 |
"%(minute_of_hour)02d", "minute_of_hour"), |
| 200 |
(r"(?<=^hh:)mm", r"(?P<minute_of_hour>[0-9][0-9])", |
| 201 |
"%(minute_of_hour)02d", "minute_of_hour"), |
| 202 |
(r"(?<=^-)mm", r"(?P<minute_of_hour>[0-9][0-9])", |
| 203 |
"%(minute_of_hour)02d", "minute_of_hour"), |
| 204 |
(r"^hh", r"(?P<hour_of_day>[0-9][0-9])", |
| 205 |
"%(hour_of_day)02d", "hour_of_day"), |
| 206 |
(r",ii", r",(?P<hour_of_day_decimal>[0-9]+)", |
| 207 |
",%(hour_of_day_decimal_string)s", "hour_of_day_decimal_string"), |
| 208 |
(r"\.ii", r"\.(?P<hour_of_day_decimal>[0-9]+)", |
| 209 |
".%(hour_of_day_decimal_string)s", "hour_of_day_decimal_string"), |
| 210 |
(r",nn", r",(?P<minute_of_hour_decimal>[0-9]+)", |
| 211 |
",%(minute_of_hour_decimal_string)s", "minute_of_hour_decimal_string"), |
| 212 |
(r"\.nn", r"\.(?P<minute_of_hour_decimal>[0-9]+)", |
| 213 |
".%(minute_of_hour_decimal_string)s", "minute_of_hour_decimal_string"), |
| 214 |
(r"ss", r"(?P<second_of_minute>[0-9][0-9])", |
| 215 |
"%(second_of_minute)02d", "second_of_minute"), |
| 216 |
(r",tt", r",(?P<second_of_minute_decimal>[0-9]+)", |
| 217 |
",%(second_of_minute_decimal_string)s", |
| 218 |
"second_of_minute_decimal_string"), |
| 219 |
(r"\.tt", r"\.(?P<second_of_minute_decimal>[0-9]+)", |
| 220 |
".%(second_of_minute_decimal_string)s", |
| 221 |
"second_of_minute_decimal_string"), |
| 222 |
(r"^--", r"(?P<truncated>--)", |
| 223 |
"--", None), |
| 224 |
(r"^-", r"(?P<truncated>-)", |
| 225 |
"-", None) |
| 226 |
] |
| 227 |
_TIME_ZONE_TRANSLATE_INFO = [ |
| 228 |
(r"mm", r"(?P<time_zone_minute>[0-9][0-9])", |
| 229 |
"%(time_zone_minute_abs)02d", "time_zone_minute_abs"), |
| 230 |
(r"mm", r"(?P<time_zone_minute>[0-9][0-9])", |
| 231 |
"%(time_zone_minute_abs)02d", "time_zone_minute_abs"), |
| 232 |
(r"hh", r"(?P<time_zone_hour>[0-9][0-9])", |
| 233 |
"%(time_zone_hour_abs)02d", "time_zone_hour_abs"), |
| 234 |
(r"\+", r"(?P<time_zone_sign>[-+])", |
| 235 |
"%(time_zone_sign)s", "time_zone_sign"), |
| 236 |
(r"Z", r"(?P<time_zone_utc>Z)", |
| 237 |
"Z", None) |
| 238 |
] |
| 239 |
|
| 240 |
LOCAL_TIME_ZONE_BASIC = timezone.get_local_time_zone_format() |
| 241 |
LOCAL_TIME_ZONE_BASIC_NO_Z = LOCAL_TIME_ZONE_BASIC |
| 242 |
if LOCAL_TIME_ZONE_BASIC_NO_Z == "Z": |
| 243 |
LOCAL_TIME_ZONE_BASIC_NO_Z = "+0000" |
| 244 |
LOCAL_TIME_ZONE_EXTENDED = timezone.get_local_time_zone_format( |
| 245 |
timezone.TimeZoneFormatMode.extended) |
| 246 |
LOCAL_TIME_ZONE_EXTENDED_NO_Z = LOCAL_TIME_ZONE_EXTENDED |
| 247 |
if LOCAL_TIME_ZONE_EXTENDED_NO_Z == "Z": |
| 248 |
LOCAL_TIME_ZONE_EXTENDED_NO_Z = "+0000" |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
REC_SPLIT_STRFTIME_DIRECTIVE = re.compile(r"(%\w)") |
| 253 |
REC_STRFTIME_DIRECTIVE_TOKEN = re.compile(r"^%\w$") |
| 254 |
STRFTIME_TRANSLATE_INFO = { |
| 255 |
"%d": ["day_of_month"], |
| 256 |
"%F": ["century", "year_of_century", "-", "month_of_year", "-", |
| 257 |
"day_of_month"], |
| 258 |
"%H": ["hour_of_day"], |
| 259 |
"%j": ["day_of_year"], |
| 260 |
"%m": ["month_of_year"], |
| 261 |
"%M": ["minute_of_hour"], |
| 262 |
"%s": ( |
| 263 |
r"(?P<seconds_since_unix_epoch>[0-9]+[,.]?[0-9]*)", |
| 264 |
"%(seconds_since_unix_epoch)s", "seconds_since_unix_epoch"), |
| 265 |
"%S": ["second_of_minute"], |
| 266 |
"%X": ["hour_of_day", ":", "minute_of_hour", ":", "second_of_minute"], |
| 267 |
"%Y": ["century", "year_of_century"], |
| 268 |
"%z": ["time_zone_sign", "time_zone_hour_abs", "time_zone_minute_abs"], |
| 269 |
} |
| 270 |
STRPTIME_EXCLUSIVE_GROUP_INFO = { |
| 271 |
"%X": ("%H", "%M", "%S"), |
| 272 |
"%F": ("%Y", "%y", "%m", "%d"), |
| 273 |
"%s": tuple(i for i in STRFTIME_TRANSLATE_INFO if i != "%s") |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
def get_date_translate_info(num_expanded_year_digits=2): |
| 278 |
"""Return list of 4-element tuples with date translate information. |
| 279 |
|
| 280 |
returns: |
| 281 |
list: List tuples. Each tuple has 4 elements: |
| 282 |
- regex1 (str) - regex to match a date info substitution string |
| 283 |
- regex2 (str) - regex to capture date info |
| 284 |
- format (str) - template string to format date info |
| 285 |
- name (str) - name of this property |
| 286 |
""" |
| 287 |
expanded_year_digit_regex = r"[0-9]" * num_expanded_year_digits |
| 288 |
return _DATE_TRANSLATE_INFO + [ |
| 289 |
("X", |
| 290 |
"(?P<expanded_year>" + expanded_year_digit_regex + ")", |
| 291 |
"%(expanded_year_digits)0" + str(num_expanded_year_digits) + "d", |
| 292 |
"expanded_year_digits") |
| 293 |
] |
| 294 |
|
| 295 |
|
| 296 |
def get_time_translate_info(): |
| 297 |
"""Return list of 4-element tuples with time translate information. |
| 298 |
|
| 299 |
returns: |
| 300 |
list: List tuples. Each tuple has 4 elements: |
| 301 |
- regex1 (str) - regex to match a time info substitution string |
| 302 |
- regex2 (str) - regex to capture a time info |
| 303 |
- format (str) - template string to format time info |
| 304 |
- name (str) - name of this property |
| 305 |
""" |
| 306 |
return _TIME_TRANSLATE_INFO |
| 307 |
|
| 308 |
|
| 309 |
def get_time_zone_translate_info(): |
| 310 |
"""Return list of 4-element tuples with time zone translate information. |
| 311 |
|
| 312 |
returns: |
| 313 |
list: List tuples. Each tuple has 4 elements: |
| 314 |
- regex1 (str) - regex to match a time zone substitution string |
| 315 |
- regex2 (str) - regex to capture a time zone |
| 316 |
- format (str) - template string to format time zone |
| 317 |
- name (str) - name of this property |
| 318 |
""" |
| 319 |
return _TIME_ZONE_TRANSLATE_INFO |
| 320 |
|
| 321 |
|
| 322 |
def translate_strftime_token(strftime_token, num_expanded_year_digits=2): |
| 323 |
"""Convert a strftime format into our own dump format.""" |
| 324 |
return _translate_strftime_token( |
| 325 |
strftime_token, dump_mode=True, |
| 326 |
num_expanded_year_digits=num_expanded_year_digits |
| 327 |
) |
| 328 |
|
| 329 |
|
| 330 |
def translate_strptime_token(strptime_token, num_expanded_year_digits=2): |
| 331 |
"""Convert a strptime format into our own parsing format.""" |
| 332 |
return _translate_strftime_token( |
| 333 |
strptime_token, dump_mode=False, |
| 334 |
num_expanded_year_digits=num_expanded_year_digits |
| 335 |
) |
| 336 |
|
| 337 |
|
| 338 |
def _translate_strftime_token(strftime_token, dump_mode=False, |
| 339 |
num_expanded_year_digits=2): |
| 340 |
if strftime_token not in STRFTIME_TRANSLATE_INFO: |
| 341 |
raise StrftimeSyntaxError(strftime_token) |
| 342 |
our_translation = "" |
| 343 |
our_translate_info = ( |
| 344 |
get_date_translate_info( |
| 345 |
num_expanded_year_digits=num_expanded_year_digits) + |
| 346 |
get_time_translate_info() + |
| 347 |
get_time_zone_translate_info() |
| 348 |
) |
| 349 |
attr_names = STRFTIME_TRANSLATE_INFO[strftime_token] |
| 350 |
if isinstance(attr_names, str): |
| 351 |
if dump_mode: |
| 352 |
return attr_names, [] |
| 353 |
return re.escape(attr_names), [] |
| 354 |
if isinstance(attr_names, tuple): |
| 355 |
(substitute, format_, name) = attr_names |
| 356 |
if dump_mode: |
| 357 |
our_translation += format_ |
| 358 |
else: |
| 359 |
our_translation += substitute |
| 360 |
return our_translation, [name] |
| 361 |
attr_names = list(attr_names) |
| 362 |
for attr_name in list(attr_names): |
| 363 |
for _, substitute, format_, name in our_translate_info: |
| 364 |
if name == attr_name: |
| 365 |
if dump_mode: |
| 366 |
our_translation += format_ |
| 367 |
else: |
| 368 |
our_translation += substitute |
| 369 |
break |
| 370 |
else: |
| 371 |
|
| 372 |
our_translation += attr_name |
| 373 |
attr_names.remove(attr_name) |
| 374 |
return our_translation, attr_names |