Skip to content

Fix from_timestamp for negative timestamps before epoch#962

Open
parinporecha wants to merge 3 commits into
python-pendulum:masterfrom
parinporecha:fix/from-timestamp-negative-values
Open

Fix from_timestamp for negative timestamps before epoch#962
parinporecha wants to merge 3 commits into
python-pendulum:masterfrom
parinporecha:fix/from-timestamp-negative-values

Conversation

@parinporecha

@parinporecha parinporecha commented Apr 19, 2026

Copy link
Copy Markdown

Problem

pendulum.from_timestamp() raises on Windows 11 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 OSError or OverflowError for these values.

The same underlying limitation also affects the public classmethods:

  • pendulum.DateTime.fromtimestamp()
  • pendulum.DateTime.utcfromtimestamp()

Reported in issue #956.

Reproduction

>>> pendulum.from_timestamp(-43200)
DateTime(1969, 12, 31, 12, 0, 0, tzinfo=Timezone('UTC'))

>>> pendulum.from_timestamp(-43201)
OSError: [Errno 22] Invalid argument

Fix

Catch (OSError, OverflowError) from the stdlib timestamp constructors and fall back to computing the result from a fixed Unix epoch plus a timedelta.

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:

  • negative timestamps before the epoch
  • explicit timezone conversion for negative timestamps
  • float timestamps preserving microseconds
  • forced fallback coverage for pendulum.from_timestamp()
  • forced fallback coverage for DateTime.fromtimestamp()
  • forced fallback coverage for DateTime.utcfromtimestamp()
  • both OSError and OverflowError fallback paths

Targeted 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' -q

The full suite also passes locally on Windows 11.

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.
@codspeed-hq

codspeed-hq Bot commented Apr 20, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 1 untouched benchmark


Comparing parinporecha:fix/from-timestamp-negative-values (e07b164) with master (0a88aec)

Open in CodSpeed
@parinporecha parinporecha force-pushed the fix/from-timestamp-negative-values branch from a3af417 to e07b164 Compare April 20, 2026 13:26
@parinporecha parinporecha reopened this Apr 22, 2026
@parinporecha parinporecha marked this pull request as ready for review April 22, 2026 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant