Skip to content

Commit

Permalink
Highlight datetime.timedelta.seconds vs .total_seconds() in docs. (
Browse files Browse the repository at this point in the history
…pythonGH-124811)

Thanks to the reviewers for suggesting the use of a "caution" section instead of "warning" or "note".
(cherry picked from commit d150e4a)

Co-authored-by: Gregory P. Smith <greg@krypto.org>
  • Loading branch information
gpshead authored and miss-islington committed Oct 1, 2024
1 parent e6fb7fa commit 904161f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ Instance attributes (read-only):

Between 0 and 86,399 inclusive.

.. caution::

It is a somewhat common bug for code to unintentionally use this attribute
when it is actually intended to get a :meth:`~timedelta.total_seconds`
value instead:

.. doctest::

>>> from datetime import timedelta
>>> duration = timedelta(seconds=11235813)
>>> duration.days, duration.seconds
(130, 3813)
>>> duration.total_seconds()
11235813.0

.. attribute:: timedelta.microseconds

Expand Down Expand Up @@ -351,7 +365,7 @@ Supported operations:
| | same value. (2) |
+--------------------------------+-----------------------------------------------+
| ``-t1`` | Equivalent to ``timedelta(-t1.days, |
| | -t1.seconds*, -t1.microseconds)``, |
| | -t1.seconds, -t1.microseconds)``, |
| | and to ``t1 * -1``. (1)(4) |
+--------------------------------+-----------------------------------------------+
| ``abs(t)`` | Equivalent to ``+t`` when ``t.days >= 0``, |
Expand Down

0 comments on commit 904161f

Please sign in to comment.