Lesson 2
Seconds vs Milliseconds
Ten-digit vs thirteen-digit values and how to avoid off-by-1000 bugs.
Unix time is defined in seconds, but many runtimes expose milliseconds for finer precision. Mixing the two is one of the most common timestamp bugs.
The digit-count heuristic
Rough rule of thumb in decimal form:
| Digits | Usually means | Example |
|---|---|---|
| 10 | Seconds | 1700000000 |
| 13 | Milliseconds | 1700000000000 |
Values with other lengths need context—always confirm against API documentation.
Why both exist
- Seconds — compact, enough for many logs and JWT expiry; used by
time.time()in Python, many C APIs, and Linuxdate +%s - Milliseconds — JavaScript
Date.now(), JavaSystem.currentTimeMillis(), some JSON APIs for sub-second ordering
Converting between them is multiplication or division by 1000—when you pick the wrong direction, dates jump to 1970 or the far future.
Fractional seconds
Some systems return floating-point seconds (1700000000.123). That is still “seconds + fraction,” not milliseconds. Parsing it as an integer millisecond field loses precision or fails validation.
Defensive habits
- Read the API schema or sample payload first
- If a date looks wrong by ~1000×, swap units mentally
- Store internally in one convention (often ms or µs in app code) and convert at boundaries
- In databases, prefer
TIMESTAMPTZtypes when available instead of naked integers
Key takeaway
Seconds and milliseconds name the same timeline with different scales. Know which scale your source uses before you convert or compare.