Repository navigation
Keep sub-second precision when serializing cookie timestamps - #89
Merged
dan1elt0m merged 3 commits intoOct 27, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, so2000-12-31 23:59:59.5was stored as2001-01-01 00:00:00.The timestamp is a 64-bit double on disk (
Format.date = "<d"), andmac_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:That truncated value is what gets written to the
expiry_dateandcreate_dateoffsets of the cookie record (src/binarycookies/_serialize.py:98andsrc/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.Before:
After:
Four tests were added to
tests/test_serialize.py. Before the fix they fail while the rest of the suite passes:After the fix the whole suite passes:
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):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_requiredwith 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.testaction_requiredCodeQLaction_requiredDependabot auto-mergeaction_requiredgh pr checksreports no checks for this branch, which is consistent with that: the test matrix has not run. For comparison, the sametestworkflow onmain(run 36393397724) concludessuccess, 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.