Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAX_TIMESTAMP calculation error #1095

Open
JosEnerv opened this issue Mar 1, 2022 · 2 comments
Open

MAX_TIMESTAMP calculation error #1095

JosEnerv opened this issue Mar 1, 2022 · 2 comments
Labels

Comments

@JosEnerv
Copy link

JosEnerv commented Mar 1, 2022

Issue Description

There are some issues related to the way max values are handled (partially because of how python's datetime works) :

The way how Arrow calculates the max timestamp constant value also fails on Linux in constants.py :

_MAX_TIMESTAMP = datetime.max.timestamp()

Which will raise a ValueError in non-utc timezones (timestamp() ) will try to return a timestamp in the local timezone):

ValueError: year 10000 is out of range

Therefore I suggest making this more robust and replacing this with :

datetime.max.replace(tzinfo=timezone.utc).timestamp()

Which is more in line with python documentation : https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp

Using Arrow.max has its own issues , as

Arrow.utcfromtimestamp(Arrow.max.float_timestamp)

does not behave as expected and returns a date in 1978, due to normalize_timestamp (util) division by 1000 :

def normalize_timestamp(timestamp: float) -> float:
    """Normalize millisecond and microsecond timestamps into normal timestamps."""
    if timestamp > MAX_TIMESTAMP:
        if timestamp < MAX_TIMESTAMP_MS:
            timestamp /= 1000
        elif timestamp < MAX_TIMESTAMP_US:
            timestamp /= 1_000_000
        else:
            raise ValueError(f"The specified timestamp {timestamp!r} is too large.")
    return timestamp

System Info

  • 🖥 OS name and version: Debian 11
  • 🐍 Python version: 3.9.2
  • 🏹 Arrow version: 1.2.2
@JosEnerv JosEnerv added the bug label Mar 1, 2022
@systemcatch
Copy link
Collaborator

Hi @JosEnerv, thanks for the bug report, we've struggled a lot with max timestamp calculations in the past. Can you put together a minimal example so we can try to reproduce on our end?

@JosEnerv
Copy link
Author

JosEnerv commented Mar 9, 2022

Sure, this is the example :

from datetime import datetime

import arrow.constants
from arrow import Arrow

if __name__ == '__main__':
    print(arrow.constants.MAX_TIMESTAMP) # 32503762800.0

    print(arrow.Arrow.max) # 9999-12-31T23:59:59.999999+00:00
    print(arrow.Arrow.max.timestamp()) # 253402300800.0
    a = Arrow.utcfromtimestamp(arrow.Arrow.max.timestamp())
    print(a) # 1978-01-11T21:31:40.800000+00:00

    datetime.max.timestamp()  # ValueError: year 10000 is out of range

As background info, the ticket I opened on the python bugtracker : https://bugs.python.org/issue46856

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants