Fix from_timestamp for negative timestamps before epoch#962
Draft
parinporecha wants to merge 3 commits intopython-pendulum:masterfrom
Draft
Fix from_timestamp for negative timestamps before epoch#962parinporecha wants to merge 3 commits intopython-pendulum:masterfrom
parinporecha wants to merge 3 commits intopython-pendulum:masterfrom
Conversation
On Windows (and potentially other platforms), datetime.fromtimestamp raises OSError for timestamps earlier than a platform-specific minimum around -43200 (12h before the Unix epoch). This catches the OSError and falls back to computing from the epoch datetime with a timedelta. Adds tests for negative timestamps with UTC, explicit timezone, and microsecond precision.
a3af417 to
e07b164
Compare
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()raises on some platforms for timestamps earlier than approximately 12 hours before the Unix epoch (for example-43201).Depending on the platform and Python build, the stdlib timestamp constructors can raise either
OSErrororOverflowErrorfor these values.The same underlying limitation also affects the public classmethods:
pendulum.DateTime.fromtimestamp()pendulum.DateTime.utcfromtimestamp()Reported in issue #956.
Reproduction
On some platforms the same call raises
OverflowErrorinstead.Fix
Catch
(OSError, OverflowError)from the stdlib timestamp constructors and fall back to computing the result from a fixed Unix epoch plus atimedelta.This PR covers:
pendulum.from_timestamp()pendulum.DateTime.fromtimestamp()pendulum.DateTime.utcfromtimestamp()The fallback preserves microseconds and keeps timezone conversion behavior intact.
Tests
Added and updated tests for:
pendulum.from_timestamp()DateTime.fromtimestamp()DateTime.utcfromtimestamp()OSErrorandOverflowErrorfallback pathsTargeted tests run locally:
PYTHONPATH=src pytest tests/datetime/test_create_from_timestamp.py -q PYTHONPATH=src pytest tests/datetime/test_behavior.py -k 'test_fromtimestamp_falls_back_for_negative_timestamp or test_utcfromtimestamp_falls_back_for_negative_timestamp or test_fromtimestamp or test_utcfromtimestamp' -qThe full suite was not run in this environment because unrelated test dependencies are missing (
time_machine,tzdata).