Repository navigation
fix: support negative timestamps in from_timestamp on Windows - #1096
Open
abu-tahir-0 wants to merge 1 commit into
Open
abu-tahir-0 wants to merge 1 commit into
abu-tahir-0 wants to merge 1 commit into
Conversation
datetime.fromtimestamp() delegates to the platform C library and raises OSError on Windows for timestamps before 1969-12-31 12:00:00 UTC, even though those dates are well within datetime's range. Compute the UTC value with epoch arithmetic instead, which is portable and handles all supported timestamps consistently. Add a regression test.
This branch has not been deployed
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.
Problem
pendulum.from_timestamp()raisesOSError: [Errno 22] Invalid argumenton Windows for any timestamp before the Unix epoch (issue #956). The same call works on Linux/macOS.Root Cause
datetime.fromtimestamp()delegates to the platform C library, and the Windows CRT rejects negative timestamps even when the resulting date is well withindatetime's range.Solution
Compute the datetime from the UTC epoch with
datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp). This is portable and behaves identically on all platforms for timestamps withindatetime's supported range.Testing
test_create_from_timestamp_before_epochcheckingfrom_timestamp(-43201)returns1969-12-31 11:59:59 UTC.pytestdatetime suite: 572 tests pass.Impact
Negative/pre-epoch timestamps now work consistently on Windows, matching behavior on other platforms.