approxidate.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # approxidate.py -- Parsing of Git's "approxidate" time specifications
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Parsing of Git's "approxidate" time specifications.
  22. Git uses a flexible date parser called "approxidate" that accepts various
  23. formats for specifying dates and times, including:
  24. - Relative times: "yesterday", "2 days ago", "2.weeks.ago"
  25. - Absolute dates: "2005-04-07", "2005-04-07 22:13:13"
  26. - Unix timestamps: "1234567890"
  27. - Special keywords: "now", "today", "yesterday"
  28. """
  29. __all__ = ["parse_approxidate", "parse_relative_time"]
  30. import time
  31. from datetime import datetime
  32. def parse_approxidate(time_spec: str | bytes) -> int:
  33. """Parse a Git approxidate specification and return a Unix timestamp.
  34. Args:
  35. time_spec: Time specification string. Can be:
  36. - A Unix timestamp (integer as string)
  37. - A relative time like "2 weeks ago" or "2.weeks.ago"
  38. - Special keywords: "now", "today", "yesterday"
  39. - Absolute date: "2005-04-07" or "2005-04-07 22:13:13"
  40. Returns:
  41. Unix timestamp (seconds since epoch)
  42. Raises:
  43. ValueError: If the time specification cannot be parsed
  44. """
  45. if isinstance(time_spec, bytes):
  46. time_spec = time_spec.decode("utf-8")
  47. time_spec = time_spec.strip()
  48. # Get current time
  49. now = time.time()
  50. # Handle special keywords
  51. if time_spec == "yesterday":
  52. return int(now - 86400)
  53. elif time_spec == "today":
  54. # Start of today (midnight)
  55. dt = datetime.fromtimestamp(now)
  56. dt = dt.replace(hour=0, minute=0, second=0, microsecond=0)
  57. return int(dt.timestamp())
  58. elif time_spec == "now":
  59. return int(now)
  60. # Try parsing as Unix timestamp
  61. try:
  62. return int(time_spec)
  63. except ValueError:
  64. pass
  65. # Handle relative time specifications
  66. # Supports both "2 weeks ago" and "2.weeks.ago" formats
  67. if " ago" in time_spec or ".ago" in time_spec:
  68. seconds_ago = parse_relative_time(time_spec)
  69. return int(now - seconds_ago)
  70. # Try parsing as absolute timestamp formats
  71. # Git supports various formats like:
  72. # - "2005-04-07" (ISO date)
  73. # - "2005-04-07 22:13:13" (ISO datetime)
  74. # - "2005-04-07T22:13:13" (ISO 8601)
  75. formats = [
  76. "%Y-%m-%d %H:%M:%S",
  77. "%Y-%m-%dT%H:%M:%S",
  78. "%Y-%m-%d",
  79. "%Y/%m/%d %H:%M:%S",
  80. "%Y/%m/%d",
  81. ]
  82. for fmt in formats:
  83. try:
  84. dt = datetime.strptime(time_spec, fmt)
  85. return int(dt.timestamp())
  86. except ValueError:
  87. continue
  88. raise ValueError(f"Unable to parse time specification: {time_spec!r}")
  89. def parse_relative_time(time_str: str) -> int:
  90. """Parse a relative time string like '2 weeks ago' into seconds.
  91. Args:
  92. time_str: String like '2 weeks ago', '2.weeks.ago', or 'now'
  93. Returns:
  94. Number of seconds (relative to current time)
  95. Raises:
  96. ValueError: If the time string cannot be parsed
  97. """
  98. if time_str == "now":
  99. return 0
  100. # Normalize dot-separated format to space-separated
  101. # "2.weeks.ago" -> "2 weeks ago"
  102. normalized = time_str.replace(".ago", " ago").replace(".", " ")
  103. if not normalized.endswith(" ago"):
  104. raise ValueError(f"Invalid relative time format: {time_str}")
  105. parts = normalized[:-4].split()
  106. if len(parts) != 2:
  107. raise ValueError(f"Invalid relative time format: {time_str}")
  108. try:
  109. num = int(parts[0])
  110. unit = parts[1]
  111. multipliers = {
  112. "second": 1,
  113. "seconds": 1,
  114. "minute": 60,
  115. "minutes": 60,
  116. "hour": 3600,
  117. "hours": 3600,
  118. "day": 86400,
  119. "days": 86400,
  120. "week": 604800,
  121. "weeks": 604800,
  122. "month": 2592000, # 30 days
  123. "months": 2592000,
  124. "year": 31536000, # 365 days
  125. "years": 31536000,
  126. }
  127. if unit in multipliers:
  128. return num * multipliers[unit]
  129. else:
  130. raise ValueError(f"Unknown time unit: {unit}")
  131. except ValueError as e:
  132. if "invalid literal" in str(e):
  133. raise ValueError(f"Invalid number in relative time: {parts[0]}")
  134. raise