Complete worked example
python
from datetime import datetime, timedelta, timezone
def cutoff(as_of, hours):
if type(hours) is not int or not 0 <= hours <= 48:
raise ValueError("hours must be 0..48")
instant = datetime.fromisoformat(as_of)
if instant.utcoffset() is None:
raise ValueError("offset required")
return (instant.astimezone(timezone.utc) - timedelta(hours=hours)).isoformat()- Reasoning
- Derive UTC boundaries from an explicit aware instant instead of mixing local clocks and naive timestamps. Compare the stated values and failure boundary before changing the implementation.