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]:
"""
Parses an ISO 8601 timestamp string into a tuple (year, month, day, hour, minute,
second, microsecond, utc_offset_minutes).
Valid format: YYYY-MM-DDTHH:MM:SS.sss[+-]HH:MM or YYYY-MM-DDTHH:MM:SS.sss[+-]HHMM
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)
"2026-10-12T14:03:07.250+0200" -> (2026, 10, 12, 14, 3, 7, 250, 120)
Args:
s: ISO 8601 timestamp string
Returns:
Tuple of (year, month, day, hour, minute, second, microsecond, utc_offset_minutes)
Raises:
ValueError: If the input string is malformed or does not conform to ISO 8601 format
"""
# Validate input
if not isinstance(s, str):
raise ValueError("Input must be a string")
if not s or s.isspace():
raise ValueError("Empty or whitespace-only string")
# Define the regular expression pattern for ISO 8601
# Matches: YYYY-MM-DDTHH:MM:SS.sss[+-]HH:MM or YYYY-MM-DDTHH:MM:SS.sss[+-]HHMM
pattern = (
r'^(\d{4})-(\d{2})-(\d{2})T'
r'(\d{2}):(\d{2}):(\d{2})\.(\d{3})'
r'(?:[+-](\d{2}):(\d{2})|([+-])(\d{2})(\d{2}))$'
)
match = re.match(pattern, s)
if not match:
raise ValueError(f"Invalid ISO 8601 format: {s}")
# Extract components
year, month, day = int(match.group(1)), int(match.group(2)), int(match.group(3))
hour, minute, second = int(match.group(4)), int(match.group(5)), int(match.group(6))
microsecond = int(match.group(7))
# Parse UTC offset
# Check for HH:MM format
offset_hh_mm = match.group(8), match.group(9)
offset_hh_mm_val = None
if offset_hh_mm[0] is not None and offset_hh_mm[1] is not None:
offset_hh_mm_val = int(offset_hh_mm[0]) * 60 + int(offset_hh_mm[1])
elif match.group(10) is not None and match.group(11) is not None:
# HHMM format
offset_hh_mm_val = int(match.group(10)) * 60 + int(match.group(11))
else:
# No offset specified - this is invalid per ISO 8601, but we'll assume 0
# However, according to ISO 8601, offset must be present in full format
# So we raise error if no offset
raise ValueError(f"No UTC offset specified in {s}")
# Determine sign based on the offset
sign = 1
if match.group(12) == '-':
sign = -1
# The offset should be parsed from the sign and the value
# If we have HH:MM or HHMM, the sign is in the first character of the offset
# Let's reconstruct properly
offset_str = match.group(8) + match.group(9) if match.group(8) and match.group(9) else match.group(10) + match.group(11)
# Actually, let's restructure the parsing to