Skip to content

fix: support negative timestamps in from_timestamp on Windows - #1096

Open
abu-tahir-0 wants to merge 1 commit into
python-pendulum:masterfrom
abu-tahir-0:fix/from-timestamp-negative-epoch
Open

abu-tahir-0 wants to merge 1 commit into
python-pendulum:masterfrom
abu-tahir-0:fix/from-timestamp-negative-epoch

Conversation

@abu-tahir-0

Copy link
Copy Markdown

Problem

pendulum.from_timestamp() raises OSError: [Errno 22] Invalid argument on 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 within datetime'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 within datetime's supported range.

Testing

  • Added test_create_from_timestamp_before_epoch checking from_timestamp(-43201) returns 1969-12-31 11:59:59 UTC.
  • Confirmed the test fails on Windows with the original implementation and passes with the fix.
  • pytest datetime suite: 572 tests pass.

Impact

Negative/pre-epoch timestamps now work consistently on Windows, matching behavior on other platforms.

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

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

1 participant