_re.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # SPDX-License-Identifier: MIT
  2. # SPDX-FileCopyrightText: 2021 Taneli Hukkinen
  3. # Licensed to PSF under a Contributor Agreement.
  4. from __future__ import annotations
  5. from datetime import date, datetime, time, timedelta, timezone, tzinfo
  6. from functools import lru_cache
  7. import re
  8. from typing import Any
  9. from ._types import ParseFloat
  10. # E.g.
  11. # - 00:32:00.999999
  12. # - 00:32:00
  13. _TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
  14. RE_NUMBER = re.compile(
  15. r"""
  16. 0
  17. (?:
  18. x[0-9A-Fa-f](?:_?[0-9A-Fa-f])* # hex
  19. |
  20. b[01](?:_?[01])* # bin
  21. |
  22. o[0-7](?:_?[0-7])* # oct
  23. )
  24. |
  25. [+-]?(?:0|[1-9](?:_?[0-9])*) # dec, integer part
  26. (?P<floatpart>
  27. (?:\.[0-9](?:_?[0-9])*)? # optional fractional part
  28. (?:[eE][+-]?[0-9](?:_?[0-9])*)? # optional exponent part
  29. )
  30. """,
  31. flags=re.VERBOSE,
  32. )
  33. RE_LOCALTIME = re.compile(_TIME_RE_STR)
  34. RE_DATETIME = re.compile(
  35. rf"""
  36. ([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
  37. (?:
  38. [Tt ]
  39. {_TIME_RE_STR}
  40. (?:([Zz])|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))? # optional time offset
  41. )?
  42. """,
  43. flags=re.VERBOSE,
  44. )
  45. def match_to_datetime(match: re.Match) -> datetime | date:
  46. """Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
  47. Raises ValueError if the match does not correspond to a valid date
  48. or datetime.
  49. """
  50. (
  51. year_str,
  52. month_str,
  53. day_str,
  54. hour_str,
  55. minute_str,
  56. sec_str,
  57. micros_str,
  58. zulu_time,
  59. offset_sign_str,
  60. offset_hour_str,
  61. offset_minute_str,
  62. ) = match.groups()
  63. year, month, day = int(year_str), int(month_str), int(day_str)
  64. if hour_str is None:
  65. return date(year, month, day)
  66. hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
  67. micros = int(micros_str.ljust(6, "0")) if micros_str else 0
  68. if offset_sign_str:
  69. tz: tzinfo | None = cached_tz(
  70. offset_hour_str, offset_minute_str, offset_sign_str
  71. )
  72. elif zulu_time:
  73. tz = timezone.utc
  74. else: # local date-time
  75. tz = None
  76. return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
  77. @lru_cache(maxsize=None)
  78. def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
  79. sign = 1 if sign_str == "+" else -1
  80. return timezone(
  81. timedelta(
  82. hours=sign * int(hour_str),
  83. minutes=sign * int(minute_str),
  84. )
  85. )
  86. def match_to_localtime(match: re.Match) -> time:
  87. hour_str, minute_str, sec_str, micros_str = match.groups()
  88. micros = int(micros_str.ljust(6, "0")) if micros_str else 0
  89. return time(int(hour_str), int(minute_str), int(sec_str), micros)
  90. def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
  91. if match.group("floatpart"):
  92. return parse_float(match.group())
  93. return int(match.group(), 0)