Fix sub-second precision loss in datetime_to_timestamp#3384
Open
veeceey wants to merge 1 commit intodocker:mainfrom
Open
Fix sub-second precision loss in datetime_to_timestamp#3384veeceey wants to merge 1 commit intodocker:mainfrom
veeceey wants to merge 1 commit intodocker:mainfrom
Conversation
The previous implementation used `delta.seconds + delta.days * 24 * 3600` which discards microseconds from the timedelta, effectively flooring datetime arguments to second resolution. This causes `container.logs()` to return extra logs before `since` and miss valid logs before `until` when sub-second precision matters. Replace with `delta.total_seconds()` which correctly returns a float preserving microsecond precision. The Docker API already accepts fractional timestamps, and the callers already handle float values. Fixes docker#3342
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.
datetime_to_timestampwas usingdelta.seconds + delta.days * 24 * 3600to convert a timedelta to a Unix timestamp. The problem is thattimedelta.secondsonly returns the whole seconds component and throws away microseconds, so any sub-second precision in the input datetime gets silently floored.This means passing a datetime like
datetime(2025, 6, 1, 12, 0, 0, microsecond=500_000)assincetocontainer.logs()would behave as if you passed12:00:00.000instead of12:00:00.500, returning an extra half-second of unwanted logs.The fix is straightforward — replace the manual arithmetic with
delta.total_seconds(), which returns a float that preserves microsecond precision. The Docker Engine API already accepts fractional timestamps, and the calling code incontainer.logs()andevents()already handles float values.Added unit tests covering sub-second precision, whole-second datetimes, epoch, and naive datetimes.
Fixes #3342