Calculate a Retry-After delay using an explicit UTC clock
Python
Calculate a waiting interval from a restricted Retry-After value without sleeping, contacting a server or automatically retrying a request.
Use this deterministic helper when testing retry configuration or inspecting an HTTP response. Supply the header value, an explicit UTC reference time and an informational cap. The result reports the delay and whether it exceeds that cap. Your caller remains responsible for retry safety, scheduling and idempotency.
Before you start
Python 3.12 standard library and the reviewed pinned runtime; no third-party packages.
One UTF-8 JSON request on stdin, at most 20000 encoded bytes. One extra byte may be read to detect overflow.
Use synthetic examples when sharing requests. The program does not read configuration files, environment variables or network services.
Build and check
Save the downloaded source as calculate-retry-after-delay-python.py.
Save the example JSON request below as request.json.
Run python3 calculate-retry-after-delay-python.py < request.json.
Compare stdout with the expected JSON. Exit 0 processes a valid request; invalid input exits 2 with empty stdout and a generic stderr message.
Limits to understand
The request must contain exactly retry_after and now strings, plus an integer cap_seconds from 0 through 86400. Booleans are rejected.
now must use YYYY-MM-DDTHH:MM:SSZ. HTTP dates must use exact IMF-fixdate spelling with GMT and a matching weekday; date years are restricted to 1970 through 2100.
ASCII delta-seconds are limited to 0 through 604800. Leading zeros are accepted. For absolute dates, the computed delay may exceed seven days; past dates return zero.
exceeds_cap informs the caller but does not clamp the delay. No sleep, jitter, HTTP request or automatic retry is performed.
This intentionally restricted parser rejects obsolete HTTP-date forms and surrounding whitespace. It is not a complete general-purpose HTTP header parser. Leap seconds are rejected.
Code license: MIT. Keep the license notice with your copy.
Example version 1.0.0 · Documentation reviewed 2026-11-14.
40 independent CLI cases passed in rootless Docker on the pinned Python 3.12 image, with 512 MiB RAM, 1 CPU and no network. Includes the documented example, malformed input, Unicode and the inclusive 20000-byte boundary. Other runtimes and all possible inputs have not been verified.