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[.mmm][+|-]HH:MM
Example: "2026-10-12T14:03:07.250+02:00"
Args:
s: ISO 8601 timestamp string
Returns:
Tuple (year, month, day, hour, minute, second, microsecond, utc_offset_minutes)
Raises:
ValueError: If the string is malformed or doesn't match ISO 8601 format
"""
# Validate input
if not isinstance(s, str):
raise ValueError("Input must be a string")
if not s or s.strip() == "":
raise ValueError("Empty string provided")
# Regex pattern for ISO 8601 with optional fractional seconds and offset
pattern = (
r'^(\d{4})[-/](\d{2})[-/](\d{2})T'
r'(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?'
r'([+-])(\d{2}):(\d{2})$'
)
match = re.match(pattern, s)
if not match:
raise ValueError(f"Malformed ISO 8601 string: {s}")
# Extract groups
year, month, day, hour, minute, second, microsecond_str, sign, offset_hour, offset_minute = match.groups()
# Convert to integers
year = int(year)
month = int(month)
day = int(day)
hour = int(hour)
minute = int(minute)
second = int(second)
# Handle microsecond - if present, pad to 3 digits; if not, use 0
microsecond = int(microsecond_str) if microsecond_str else 0
# UTC offset in minutes
offset_minutes = int(offset_hour) * 60 + int(offset_minute)
offset_minutes = offset_minutes * (-1 if sign == '-' else 1)
# Validate date components (year 1-9999, month 1-12, day 1-31, etc.)
if not (1 <= month <= 12):
raise ValueError(f"Invalid month: {month}")
# Days in month (simplified validation - only basic sanity check)
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}: {day}")
# Validate time components (0-23, 0-59)
if not (0 <= hour <= 23):
raise ValueError(f"Invalid hour: {hour}")
if not (0 <= minute <= 59):
raise ValueError(f"Invalid minute: {minute}")
if not (0 <= second <= 59):
raise ValueError(f"Invalid second: {second}")
# Validate microsecond (0-999)
if microsecond < 0 or microsecond > 999:
raise ValueError(f"Invalid microsecond: {microsecond}")
# Validate offset (range of offset in minutes)
# While there's no strict limit in ISO 8601, we'll allow reasonable ranges
if offset_minutes < -1440 or offset_minutes > 1440: # ±24 hours
raise ValueError(f"Invalid UTC offset: {offset_minutes} minutes")
return (year, month, day, hour, minute, second, microsecond, offset_minutes)
# Test cases using pytest
def test_parse_iso8601_basic():
assert parse_iso8601("2026-10-12T14:03:07.250+0