Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(241320)

Unified Diff: Lib/datetime.py

Issue 17267: datetime.time support for '+' and 'now'
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
index f506e9a..eced0b2 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -1059,6 +1059,30 @@ class time:
"""timezone info object"""
return self._tzinfo
+ def __add__(self, other):
+ "Add a time and a timedelta"
sasha 2013/06/05 23:40:29 Please use tripple quotes for docstring even when
+ if not isinstance(other, timedelta):
+ return NotImplemented
+
+ delta = timedelta(hours=self._hour,
sasha 2013/06/05 23:40:29 I would just use a dummy date and do something lik
+ minutes=self._minute,
+ seconds=self._second,
+ microseconds=self._microsecond)
+
+ delta += other
+ hour, rem = divmod(delta.seconds, 3600)
+ minute, second = divmod(rem, 60)
+
+ return time(hour, minute, second,
+ delta.microseconds,
+ tzinfo=self._tzinfo)
+
+ def __sub__(self, other):
+ "Subtract a time and a timedelta."
+ if isinstance(other, timedelta):
+ return self + -other
Martin Panter 2019/04/14 02:28:55 This should be smarter to avoid OverflowError on s
+ return NotImplemented
+
# Standard conversions, __hash__ (and helpers)
# Comparisons of time objects with other.
@@ -1122,8 +1146,10 @@ class time:
return 2 # arbitrary non-zero value
else:
raise TypeError("cannot compare naive and aware times")
+
myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1)
othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1)
+
return _cmp((myhhmm, self._second, self._microsecond),
(othhmm, other._second, other._microsecond))
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+