Skip to content

Keep sub-second precision when serializing cookie timestamps - #89

Merged
dan1elt0m merged 3 commits into
dan1elt0m:mainfrom
JingHao-Leon:fix/sub-second-timestamp-precision
Oct 27, 2026
Merged

dan1elt0m merged 3 commits into
dan1elt0m:mainfrom
JingHao-Leon:fix/sub-second-timestamp-precision

Conversation

@JingHao-Leon

@JingHao-Leon JingHao-Leon commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What was broken

dumps() / dump() truncated the creation and expiry timestamps to whole seconds, so writing a cookie out and reading it back did not return the timestamps it started with. Dates before the Mac epoch (2001-01-01) were rounded towards the Mac epoch instead of being truncated, so 2000-12-31 23:59:59.5 was stored as 2001-01-01 00:00:00.

The timestamp is a 64-bit double on disk (Format.date = "<d"), and mac_epoch_to_date() already keeps the fractional part when reading, so the writer was the only side that lost it.

Root cause

src/binarycookies/_serialize.py:34 — date_to_mac_epoch() truncated the number of seconds to an integer:

return int((date - mac_epoch_start).total_seconds())

That truncated value is what gets written to the expiry_date and create_date offsets of the cookie record (src/binarycookies/_serialize.py:98 and src/binarycookies/_serialize.py:99).

Because int() truncates towards zero, a negative number of seconds — a date before 2001-01-01 — was rounded up towards the Mac epoch rather than truncated.

How this was verified

date_to_mac_epoch() now returns the exact number of seconds, so the double written to the file is the one that is read back.

from datetime import datetime, timezone
from io import BytesIO
from binarycookies import dumps, load

def show(created):
    cookie = {
        "name": "session_id", "value": "abc123", "url": "example.com", "path": "/",
        "flag": "Secure", "create_datetime": created.isoformat(),
        "expiry_datetime": "2033-12-31T23:59:59+00:00",
    }
    [r] = load(BytesIO(dumps([cookie])))
    return r.create_datetime

print("in : 2023-10-01 12:34:56.789000+00:00 -> out:", show(datetime(2023, 10, 1, 12, 34, 56, 789000, tzinfo=timezone.utc)))
print("in : 2000-12-31 23:59:59.500000+00:00 -> out:", show(datetime(2000, 12, 31, 23, 59, 59, 500000, tzinfo=timezone.utc)))

Before:

in : 2023-10-01 12:34:56.789000+00:00 -> out: 2023-10-01 12:34:56+00:00
in : 2000-12-31 23:59:59.500000+00:00 -> out: 2001-01-01 00:00:00+00:00

After:

in : 2023-10-01 12:34:56.789000+00:00 -> out: 2023-10-01 12:34:56.789000+00:00
in : 2000-12-31 23:59:59.500000+00:00 -> out: 2000-12-31 23:59:59.500000+00:00

Four tests were added to tests/test_serialize.py. Before the fix they fail while the rest of the suite passes:

$ pytest -q
FAILED tests/test_serialize.py::test_date_to_mac_epoch_keeps_fractional_seconds
FAILED tests/test_serialize.py::test_dumps_stores_fractional_seconds_in_the_timestamp_double
FAILED tests/test_serialize.py::test_round_trip_preserves_sub_second_precision
FAILED tests/test_serialize.py::test_round_trip_dates_before_the_mac_epoch
4 failed, 27 passed in 0.35s

After the fix the whole suite passes:

$ pytest -q
31 passed in 0.36s

The suite was also run against both pydantic versions in the CI matrix (1.10.26 and 2.14.1): 31 passed with each.

Lint and formatting are clean with the ruff version pinned in poetry.lock (0.7.4):

$ ruff check .
All checks passed!
$ ruff format --check .
13 files already formatted

CI status

The workflows for this pull request have not executed a single job yet. All three runs on this branch are sitting at action_required with zero jobs, which is the "Approve and run workflows" gate that applies to a first-time contributor — a maintainer has to approve them before they start.

Workflow Run Conclusion Jobs
test 39336133322 action_required 0
CodeQL 39336133325 action_required 0
Dependabot auto-merge 39336133377 action_required 0

gh pr checks reports no checks for this branch, which is consistent with that: the test matrix has not run. For comparison, the same test workflow on main (run 36393397724) concludes success, so this is the approval gate rather than a failing suite.

The verification above was therefore done locally: Python 3.13.9, pydantic 1.10.26 and 2.14.1, and ruff 0.7.4 (the version pinned in poetry.lock).

Fixes #88

This change was prepared with the assistance of an AI coding agent.

JingHao-Leon and others added 3 commits October 20, 2026 06:26
The Mac epoch timestamp is stored on disk as a 64-bit double, and the
reader keeps the fractional part, but the writer truncates the value to
whole seconds. Add regression tests covering the sub-second round trip
and dates before the Mac epoch.
The creation and expiry timestamps are stored on disk as 64-bit doubles
and the reader preserves the fractional part, but the writer truncated the
value to whole seconds. Writing a cookie out therefore moved its
timestamps, and dates before the Mac epoch were rounded towards 2001-01-01
instead of being truncated.

Return the exact number of seconds so the value written matches the
double that is read back.
@dan1elt0m
dan1elt0m merged commit e52696a into dan1elt0m:main Oct 27, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sub-second precision is lost when writing cookie timestamps

2 participants