dateparse.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Functions to parse datetime objects."""
  2. # We're using regular expressions rather than time.strptime because:
  3. # - They provide both validation and parsing.
  4. # - They're more flexible for datetimes.
  5. # - The date/datetime/time constructors produce friendlier error messages.
  6. import datetime
  7. import re
  8. from django.utils.timezone import get_fixed_timezone, utc
  9. date_re = re.compile(
  10. r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$'
  11. )
  12. time_re = re.compile(
  13. r'(?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
  14. r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
  15. )
  16. datetime_re = re.compile(
  17. r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
  18. r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
  19. r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
  20. r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
  21. )
  22. standard_duration_re = re.compile(
  23. r'^'
  24. r'(?:(?P<days>-?\d+) (days?, )?)?'
  25. r'(?P<sign>-?)'
  26. r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
  27. r'(?:(?P<minutes>\d+):)?'
  28. r'(?P<seconds>\d+)'
  29. r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
  30. r'$'
  31. )
  32. # Support the sections of ISO 8601 date representation that are accepted by
  33. # timedelta
  34. iso8601_duration_re = re.compile(
  35. r'^(?P<sign>[-+]?)'
  36. r'P'
  37. r'(?:(?P<days>\d+(.\d+)?)D)?'
  38. r'(?:T'
  39. r'(?:(?P<hours>\d+(.\d+)?)H)?'
  40. r'(?:(?P<minutes>\d+(.\d+)?)M)?'
  41. r'(?:(?P<seconds>\d+(.\d+)?)S)?'
  42. r')?'
  43. r'$'
  44. )
  45. # Support PostgreSQL's day-time interval format, e.g. "3 days 04:05:06". The
  46. # year-month and mixed intervals cannot be converted to a timedelta and thus
  47. # aren't accepted.
  48. postgres_interval_re = re.compile(
  49. r'^'
  50. r'(?:(?P<days>-?\d+) (days? ?))?'
  51. r'(?:(?P<sign>[-+])?'
  52. r'(?P<hours>\d+):'
  53. r'(?P<minutes>\d\d):'
  54. r'(?P<seconds>\d\d)'
  55. r'(?:\.(?P<microseconds>\d{1,6}))?'
  56. r')?$'
  57. )
  58. def parse_date(value):
  59. """Parse a string and return a datetime.date.
  60. Raise ValueError if the input is well formatted but not a valid date.
  61. Return None if the input isn't well formatted.
  62. """
  63. match = date_re.match(value)
  64. if match:
  65. kw = {k: int(v) for k, v in match.groupdict().items()}
  66. return datetime.date(**kw)
  67. def parse_time(value):
  68. """Parse a string and return a datetime.time.
  69. This function doesn't support time zone offsets.
  70. Raise ValueError if the input is well formatted but not a valid time.
  71. Return None if the input isn't well formatted, in particular if it
  72. contains an offset.
  73. """
  74. match = time_re.match(value)
  75. if match:
  76. kw = match.groupdict()
  77. kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
  78. kw = {k: int(v) for k, v in kw.items() if v is not None}
  79. return datetime.time(**kw)
  80. def parse_datetime(value):
  81. """Parse a string and return a datetime.datetime.
  82. This function supports time zone offsets. When the input contains one,
  83. the output uses a timezone with a fixed offset from UTC.
  84. Raise ValueError if the input is well formatted but not a valid datetime.
  85. Return None if the input isn't well formatted.
  86. """
  87. match = datetime_re.match(value)
  88. if match:
  89. kw = match.groupdict()
  90. kw['microsecond'] = kw['microsecond'] and kw['microsecond'].ljust(6, '0')
  91. tzinfo = kw.pop('tzinfo')
  92. if tzinfo == 'Z':
  93. tzinfo = utc
  94. elif tzinfo is not None:
  95. offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
  96. offset = 60 * int(tzinfo[1:3]) + offset_mins
  97. if tzinfo[0] == '-':
  98. offset = -offset
  99. tzinfo = get_fixed_timezone(offset)
  100. kw = {k: int(v) for k, v in kw.items() if v is not None}
  101. kw['tzinfo'] = tzinfo
  102. return datetime.datetime(**kw)
  103. def parse_duration(value):
  104. """Parse a duration string and return a datetime.timedelta.
  105. The preferred format for durations in Django is '%d %H:%M:%S.%f'.
  106. Also supports ISO 8601 representation and PostgreSQL's day-time interval
  107. format.
  108. """
  109. match = (
  110. standard_duration_re.match(value) or
  111. iso8601_duration_re.match(value) or
  112. postgres_interval_re.match(value)
  113. )
  114. if match:
  115. kw = match.groupdict()
  116. days = datetime.timedelta(float(kw.pop('days', 0) or 0))
  117. sign = -1 if kw.pop('sign', '+') == '-' else 1
  118. if kw.get('microseconds'):
  119. kw['microseconds'] = kw['microseconds'].ljust(6, '0')
  120. if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'):
  121. kw['microseconds'] = '-' + kw['microseconds']
  122. kw = {k: float(v) for k, v in kw.items() if v is not None}
  123. return days + sign * datetime.timedelta(**kw)