Skip to content

gh-76075: Remove mentions of C's mktime in datetime.timestamp() #92796

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

slateny
Copy link
Contributor

@slateny slateny commented May 14, 2022

#76075

https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp

https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c#L6378

>>> from datetime import datetime
>>> datetime(year=1900, month=1, day=1).timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

@slateny slateny requested review from pganssle and abalkin as code owners May 14, 2022 05:29
@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels May 14, 2022
Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least on Windows, .timestamp() can continue to raise errors for legal datetimes, so I think we should keep some mention of the errors that can be raised.

A

platforms, this method may raise :exc:`OverflowError` for times far
in the past or far in the future.
returned by :func:`time.time`. Raises :exc:`OSError` for times far in the
past or far in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is maybe too terse. All the core elements of the documentation are still true, IIRC,the only difference is that timestamp uses different system level calls.

I think we want to replace the specific "calls mktime" with a more generic, "relies on system APIs, which may have a different valid range than datetime."

Also, is it not still possible to get an OverflowError?

Copy link
Contributor Author

@slateny slateny May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it does make sense to give some explanation why OSError might occur, so the generic statement sounds good to me.

As for OverflowError, I'm not sure, but I tried an old date and Adam above tried with a very future date, and both gave OSError. Overly large dates (year=999999) and otherwise invalid times (year=-1, 0) give ValueError and very small years give a negative timestamp, so not too sure how I'd be able to replicate OverflowError

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On WSL Ubuntu, Python 3.10:

>>> datetime.datetime(9_999, 12, 31).timestamp()
253402214400.0
>>> datetime.datetime(1, 1, 1).timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year 0 is out of range
>>> datetime.datetime(1, 1, 2).timestamp()
-62135510325.0

The ValueError for datetime.datetime(1, 1, 1).timestamp() is very odd, as datetime.datetime(1, 1, 1) is legal -- perhaps a bug in the implementation?

A

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ValueError for datetime.datetime(1, 1, 1).timestamp() is very odd, as datetime.datetime(1, 1, 1) is legal -- perhaps a bug in the implementation?

I think that's this.

Overly large dates (year=999999) and otherwise invalid times (year=-1, 0) give ValueError and very small years give a negative timestamp, so not too sure how I'd be able to replicate OverflowError

Possibly this only occurs on 32-bit platforms? There's a comment here that indicates that might be the issue?

Copy link
Contributor Author

@slateny slateny May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually on second glance, ValueError is for datetime(...) (which has a limit of 9999) and not timestamp, so my mistake there. As for the 32-bit platform, that's a good point as I've only tested on 64-bit

Copy link
Contributor Author

@slateny slateny May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit (need to expand the diff of _datetimemodule.c) seems to have removed mktime along with OverflowError

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

4 participants