reference: E1 greedy, r0 · 621 tokens · r1–3, r5, r7, r9 identical
Here's a Python function `parse_iso8601(s: str)` that parses an ISO 8601 timestamp into a tuple without using the `datetime` module. It includes validation and a few pytest test cases.
```python
def parse_iso8601(s: str) -> tuple:
"""
Parses an ISO 8601 timestamp string into a tuple (year, month, day, hour, minute, second, microsecond, utc_offset_minutes).
Args:
s (str): The ISO 8601 timestamp string.
Returns:
tuple: A tuple containing the parsed year, month, day, hour, minute, second, microsecond, and utc_offset_minutes.
Raises:
ValueError: If the input string does not match the expected format.
"""
# Validate input
if not s.isdigit() or not s.startswith('t'):
raise ValueError("Input must be a valid ISO 8601 timestamp string.")
# Split the string into year, month, day, hour, minute, second, microsecond, and utc_offset_minutes
parts = s.split('t')
if len(parts) != 4:
raise ValueError("Invalid input format. Expected format: year-month-day-hour-minute-second")
year = int(parts[0])
month = int(parts[1])
day = int(parts[2])
hour = int(parts[3])
minute = int(parts[4])
second = int(parts[5])
microsecond = int(parts[6])
utc_offset_minutes = int(parts[7])
return (year, month, day, hour, minute, second, microsecond, utc_offset_minutes)
# Pytest test cases
def test_parse_iso8601():
assert parse_iso8601("2026-10-12T14:03:07.250+02:00") == (2026, 10, 12, 14, 3, 7, 250, 0)
assert parse_iso8601("invalid-format") == ValueError
assert parse_iso8601("2026-10-12T14:03:02.000") == ValueError
assert parse_iso8601("2026-10-12T14:03:02.000+02:00") == (2026, 10, 12, 2, 3, 7, 0, 2)
assert parse_iso8601("2026-10-12T14:03:00") == ValueError
assert parse_iso8601("2026-10-12.000") == ValueError
```
These test cases cover various scenarios, including valid and invalid inputs, to ensure the function behaves as expected.
r0 · differs from token 596 · 670 tokens
…:00") == (2026, 10, 12, 2, 3, 7, 0, 2)
assert parse_iso8601("2026-10-12T14:03:00") == ValueError
assert parse_iso8601("2026-10-12.000") == ValueError
# Run tests
if __name__ == "__main__":
pytest.main()
```
This function uses string manipulation to split the input string into its components and then validates the format. It raises a `ValueError` if the input does not match the expected format. The test cases cover various scenarios, including valid and invalid inputs.
r4 · differs from token 246 · 749 tokens
…"Input must be a valid ISO 8601 timestamp string.")
# Split the string into year, month, day, hour, minute, second, microsecond, and utc_offset_minutes
year, month, day, hour, minute, second, microsecond = s.split('t')
# Validate each component
if not year.isdigit() or not month.isdigit() or not day.isdigit() or not hour.isdigit() or not minute.isdigit() or not second.isdigit() or not microsecond.isdigit():
raise ValueError("Each component of the ISO 8601 timestamp must be a valid number.")
# Convert the components to integers
year = int(year)
month = int(month)
day = int(day)
hour = int(hour)
minute = int(minute)
second = int(second)
microsecond = int(microsecond)
utc_offset_minutes = int(utc_offset_minutes)
# Return the parsed tuple
return (year, month, day, hour, minute, second, microsecond, utc_offset_minutes)
# Test cases
def test_parse_iso8601():
assert parse_iso8601("2026-10-12T14:03:07.250+02:00") == (2026, 10, 12, 14, 3, 7, 250, 0)
assert parse_iso8601("invalid-format") == (None, None, None, None, None, None, 0)
assert parse_iso8601("2026-10-12T14:03:02.000") == (2026, 10, 12, 14, 2, 0, 0)
assert parse_iso8601("2026.10.12.14.03.07") == (2026, 10, 12, 14, 3, 7, 250)
assert parse_iso8601("2026-10-12.000") == (None, None, None, None, 0, 0)
# Run tests
if __name__ == "__main__":
pytest.main()
```
This function uses string manipulation to split the input string into its components and then validates each component to ensure they are valid numbers. The parsed components are then converted to integers and returned as a tuple. The test cases cover various scenarios, including valid and invalid inputs.
r6, r8 · differs from token 585 · 621 tokens
…1("2026-10-12T14:03:02.000+02:00") == (2026, 10, 12, 2, 3, 7, 0, 2)
assert parse_iso8601("2026-10-12T14:03:00") == ValueError
assert parse_iso8601("2026.10.12.00") == ValueError
```
These test cases cover various scenarios, including valid and invalid inputs, to ensure the function behaves as expected.