Skip to content

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

Closed
slateny wants to merge 2 commits into
python:mainfrom
slateny:s/datetime-mktime
Closed

gh-76075: Remove mentions of C's mktime in datetime.timestamp()#92796
slateny wants to merge 2 commits into
python:mainfrom
slateny:s/datetime-mktime

Conversation

@slateny

@slateny slateny commented May 14, 2022

Copy link
Copy Markdown
Contributor

#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 abalkin and pganssle 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

@AA-Turner AA-Turner left a comment

Copy link
Copy Markdown
Member

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

Comment thread Doc/library/datetime.rst
Comment thread Doc/library/datetime.rst
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
Copy Markdown
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?

@slateny slateny May 15, 2022

Copy link
Copy Markdown
Contributor Author

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

@slateny slateny May 15, 2022

Copy link
Copy Markdown
Contributor Author

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

@slateny slateny May 20, 2022

Copy link
Copy Markdown
Contributor Author

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

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

CLA signed

@brianschubert

Copy link
Copy Markdown
Contributor

Closing as superseded by #131202. Thanks for the contribution nonetheless!

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.

5 participants