aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Doc/library/datetime.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/datetime.rst')
-rw-r--r--Doc/library/datetime.rst16
1 files changed, 16 insertions, 0 deletions
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index 1ce2013f05d..3470f42a6c6 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -261,6 +261,22 @@ A :class:`timedelta` object represents a duration, the difference between two
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)
+ Since the string representation of :class:`!timedelta` objects can be confusing,
+ use the following recipe to produce a more readable format:
+
+ .. code-block:: pycon
+
+ >>> def pretty_timedelta(td):
+ ... if td.days >= 0:
+ ... return str(td)
+ ... return f'-({-td!s})'
+ ...
+ >>> d = timedelta(hours=-1)
+ >>> str(d) # not human-friendly
+ '-1 day, 23:00:00'
+ >>> pretty_timedelta(d)
+ '-(1:00:00)'
+
Class attributes: