Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,8 @@ Instance methods:

Return POSIX timestamp corresponding to the :class:`.datetime`
instance. The return value is a :class:`float` similar to that
returned by :func:`time.time`.

Naive :class:`.datetime` instances are assumed to represent local
time and this method relies on the platform C :c:func:`mktime`
function to perform the conversion. Since :class:`.datetime`
supports wider range of values than :c:func:`mktime` on many
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


For aware :class:`.datetime` instances, the return value is computed
as::
Expand Down