Give agent-cost report a start date of not-a-date and it tells you which argument it couldn't read. Give version 0.5.0 a timestamp Python can read, 0001-01-01T00:00:00+23:59, and you get a traceback instead. What happened between reading the argument and running the report?
Move the date forward one day
You can reproduce this with Python’s standard library. Here’s the example I ran on Python 3.12:
from datetime import datetime, timezone
dt = datetime.fromisoformat("0001-01-01T00:00:00+23:59")
print(dt.isoformat()) # 0001-01-01T00:00:00+23:59
print(dt.astimezone(timezone.utc)) # OverflowError: date value out of range
The offset is the clue. To get UTC from midnight at +23:59, subtract 23 hours and 59 minutes. Try January 2 of year 1 and you land at 00:01 on January 1. Now try January 1. The arithmetic needs the preceding day, but January 1 of year 1 is the earliest date a Python datetime can represent.
The year, month, day, and offset all fit when fromisoformat() reads the string. It’s the UTC result that doesn’t fit. You can hit the other end of the range, too: converting 9999-12-31T23:59:59-12:00 to UTC adds 12 hours and runs past the end of year 9999.
There doesn't have to be an offset in the string
One of the regression cases looks quite different on the command line:
agent-cost report --since 0001-01-01 --timezone Asia/Tokyo
For a date without a time, the CLI uses midnight in the requested time zone. Midnight in Tokyo falls on the preceding day in UTC. So a date-only argument can cross the same boundary, even though there’s no offset to spot in the input.
In 0.5.0 we already caught ValueError around datetime.fromisoformat(). That handled not-a-date. The call to astimezone() came after the exception handler. Version 0.5.1 catches OverflowError at that second step, for both an explicit offset and a time zone supplied by the CLI. Patched function
Here’s what the caller now gets for the original timestamp:
$ agent-cost report --since '0001-01-01T00:00:00+23:59'
[error] invalid date/time: '0001-01-01T00:00:00+23:59' (out of range)
Both versions exit with code 1
I ran the old and new date-bound functions in separate Python processes with the year-1 timestamp. Both exited with code 1. Both left stdout empty. The difference was on stderr: the old traceback ended in OverflowError: date value out of range, while the new message included the actual argument I needed to change.
The regression test goes a step further and invokes the CLI in a subprocess. Its baseline is the existing error for --since not-a-date. For each of the three boundary cases it checks the same nonzero exit code, empty stdout, and an error message on stderr that names the rejected value and contains no traceback. Regression test
An assertion that the command failed would have passed before the fix. Checking what it printed is what tells us whether someone running the report can do anything about the failure. agent-cost PR #20
Top comments (0)