Skip to content

Fix from_timestamp for negative timestamps before epoch#962

Draft
parinporecha wants to merge 3 commits intopython-pendulum:masterfrom
parinporecha:fix/from-timestamp-negative-values
Draft

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

Conversation

@parinporecha
Copy link
Copy Markdown

@parinporecha parinporecha commented Apr 19, 2026

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

On some platforms the same call raises OverflowError instead.

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 was not run in this environment because unrelated test dependencies are missing (time_machine, tzdata).

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
Copy link
Copy Markdown

codspeed-hq bot commented Apr 20, 2026

Merging this PR will not alter performance

✅ 1 untouched benchmark


Comparing parinporecha:fix/from-timestamp-negative-values (4365af0) 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
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