Skip to content

from_timestamp fails with timestamps earlier than 12h before unix epoch #956

@dr-eme

Description

@dr-eme
  • I am on the latest Pendulum version.

  • I have searched the issues of this repo and believe that this is not a duplicate.

  • OS version and name: Win11

  • Pendulum version: 3.2.0

Issue

pendulum.from_timestamp() throws OSError when attempting to convert a timestamps earlier than 12h before the unix epoch.

>>> pendulum.from_timestamp(-43200)
DateTime(1969, 12, 31, 12, 0, 0, tzinfo=Timezone('UTC'))
>>> pendulum.from_timestamp(-43201)
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    from_timestamp(-43201)
    ~~~~~~~~~~~~~~^^^^^^^^
  File "C:\sst\reportsgenservice\.venv\Lib\site-packages\pendulum\__init__.py", line 290, in from_timestamp
    dt = _datetime.datetime.fromtimestamp(timestamp, tz=UTC)
OSError: [Errno 22] Invalid argument

However, pendulum supports these dates without issues, for instance when parsing 0 dates from a database:

>>> pendulum.parse('1900-01-01')
DateTime(1900, 1, 1, 0, 0, 0, tzinfo=Timezone('UTC'))

Current workaround is to substract negative timestamps to an unix epoch datetime instance:

>>> pendulum.from_timestamp(0).add(seconds=-43201)
DateTime(1969, 12, 31, 11, 59, 59, tzinfo=Timezone('UTC'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions