reference = r0 · 1024 tokens · r0–9 identical
```python
import re
from typing import Tuple, ValueError
def parse_iso8601(s: str) -> Tuple[int, int, int, int, int, int, int, int]:
"""
Parse an ISO 8601 timestamp string into a tuple (year, month, day, hour, minute, second, microsecond, utc_offset_minutes).
Input format: 'YYYY-MM-DDTHH:MM:SS.sssZ' or 'YYYY-MM-DDTHH:MM:SS.sss+HH:MM' or 'YYYY-MM-DDTHH:MM:SS.sss-HH:MM'
Examples:
'2026-10-12T14:03:07.250+02:00' -> (2026, 10, 12, 14, 3, 7, 250, 120)
'2026-10-12T14:03:07.250-02:00' -> (2026, 10, 12, 14, 3, 7, 250, -120)
Raises:
ValueError: If the input string is malformed or does not match ISO 8601 format.
"""
if not isinstance(s, str):
raise ValueError("Input must be a string")
# Validate basic structure using regex
pattern = r'^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})((\+|\-)(\d{2}):(\d{2}))?$'
match = re.match(pattern, s)
if not match:
raise ValueError("Invalid ISO 8601 format")
# Extract components
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
second = int(match.group(6))
microsecond = int(match.group(7))
# UTC offset parsing
offset_part = match.group(8)
utc_offset_minutes = 0
if offset_part:
# Group contains either +HH:MM or -HH:MM
sign = match.group(9) if match.group(9) else '+'
hour_offset = int(match.group(10))
minute_offset = int(match.group(11))
utc_offset_minutes = (hour_offset * 60) + (minute_offset * 1)
utc_offset_minutes = utc_offset_minutes if sign == '+' else -utc_offset_minutes
# Validate date parts (valid month and day ranges)
if month < 1 or month > 12:
raise ValueError("Invalid month")
# Days in each month (non-leap year)
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month == 2 and (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
days_in_month[1] = 29
if day < 1 or day > days_in_month[month - 1]:
raise ValueError(f"Invalid day for month {month}")
# Validate time parts
if hour < 0 or hour > 23:
raise ValueError("Invalid hour")
if minute < 0 or minute > 59:
raise ValueError("Invalid minute")
if second < 0 or second > 59:
raise ValueError("Invalid second")
# Validate microsecond
if microsecond < 0 or microsecond > 999999:
raise ValueError("Invalid microsecond")
return (year, month, day, hour, minute, second, microsecond, utc_offset_minutes)
# Test cases for pytest
def test_parse_iso8601_valid():
# Test basic valid case
assert parse_iso8601("2026-10-12T14:03:07.250+02:00") == (2026, 10, 12, 14, 3, 7, 250, 120)
# Test negative offset
assert parse_iso8601("20