Skip to content

Fix sub-second precision loss in datetime_to_timestamp#3384

Open
veeceey wants to merge 1 commit intodocker:mainfrom
veeceey:fix/issue-3342-datetime-precision
Open

Fix sub-second precision loss in datetime_to_timestamp#3384
veeceey wants to merge 1 commit intodocker:mainfrom
veeceey:fix/issue-3342-datetime-precision

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 13, 2026

datetime_to_timestamp was using delta.seconds + delta.days * 24 * 3600 to convert a timedelta to a Unix timestamp. The problem is that timedelta.seconds only 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) as since to container.logs() would behave as if you passed 12:00:00.000 instead of 12: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 in container.logs() and events() already handles float values.

Added unit tests covering sub-second precision, whole-second datetimes, epoch, and naive datetimes.

Fixes #3342

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
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.

Flooring since and until datetime arguments down to second resolution in container.logs.

1 participant