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

Delta Between Two Patch Sets: Lib/datetime.py

Issue 17267: datetime.time support for '+' and 'now'
Left Patch Set: Created 6 years, 3 months ago
Right Patch Set: Created 6 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/test/datetimetester.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Concrete date/time and related types -- prototype implemented in Python. 1 """Concrete date/time and related types -- prototype implemented in Python.
2 2
3 See http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage 3 See http://www.zope.org/Members/fdrake/DateTimeWiki/FrontPage
4 4
5 See also http://dir.yahoo.com/Reference/calendars/ 5 See also http://dir.yahoo.com/Reference/calendars/
6 6
7 For a primer on DST, including many current DST rules, see 7 For a primer on DST, including many current DST rules, see
8 http://webexhibits.org/daylightsaving/ 8 http://webexhibits.org/daylightsaving/
9 9
10 For more about DST than you ever wanted to know, see 10 For more about DST than you ever wanted to know, see
(...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after
1053 def microsecond(self): 1053 def microsecond(self):
1054 """microsecond (0-999999)""" 1054 """microsecond (0-999999)"""
1055 return self._microsecond 1055 return self._microsecond
1056 1056
1057 @property 1057 @property
1058 def tzinfo(self): 1058 def tzinfo(self):
1059 """timezone info object""" 1059 """timezone info object"""
1060 return self._tzinfo 1060 return self._tzinfo
1061 1061
1062 def __add__(self, other): 1062 def __add__(self, other):
1063 "Add a time and a timedelta" 1063 "Add a time and a timedelta"
sasha 2013/06/05 23:40:29 Please use tripple quotes for docstring even when
1064 if not isinstance(other, timedelta): 1064 if not isinstance(other, timedelta):
1065 return NotImplemented 1065 return NotImplemented
1066 1066
1067 delta = timedelta(hours=self._hour, 1067 delta = timedelta(hours=self._hour,
sasha 2013/06/05 23:40:29 I would just use a dummy date and do something lik
1068 minutes=self._minute, 1068 minutes=self._minute,
1069 seconds=self._second, 1069 seconds=self._second,
1070 microseconds=self._microsecond) 1070 microseconds=self._microsecond)
1071 1071
1072 delta += other 1072 delta += other
1073 hour, rem = divmod(delta.seconds, 3600) 1073 hour, rem = divmod(delta.seconds, 3600)
1074 minute, second = divmod(rem, 60) 1074 minute, second = divmod(rem, 60)
1075 if delta.days <= _MAXORDINAL: 1075
1076 return time(hour, minute, second, 1076 return time(hour, minute, second,
1077 delta.microseconds, 1077 delta.microseconds,
1078 tzinfo=self._tzinfo) 1078 tzinfo=self._tzinfo)
1079 raise OverflowError("result out of range") 1079
1080 def __sub__(self, other):
1081 "Subtract a time and a timedelta."
1082 if isinstance(other, timedelta):
1083 return self + -other
Martin Panter 2019/04/14 02:28:55 This should be smarter to avoid OverflowError on s
1084 return NotImplemented
1080 1085
1081 # Standard conversions, __hash__ (and helpers) 1086 # Standard conversions, __hash__ (and helpers)
1082 1087
1083 # Comparisons of time objects with other. 1088 # Comparisons of time objects with other.
1084 1089
1085 def __eq__(self, other): 1090 def __eq__(self, other):
1086 if isinstance(other, time): 1091 if isinstance(other, time):
1087 return self._cmp(other, allow_mixed=True) == 0 1092 return self._cmp(other, allow_mixed=True) == 0
1088 else: 1093 else:
1089 return False 1094 return False
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1134 if base_compare: 1139 if base_compare:
1135 return _cmp((self._hour, self._minute, self._second, 1140 return _cmp((self._hour, self._minute, self._second,
1136 self._microsecond), 1141 self._microsecond),
1137 (other._hour, other._minute, other._second, 1142 (other._hour, other._minute, other._second,
1138 other._microsecond)) 1143 other._microsecond))
1139 if myoff is None or otoff is None: 1144 if myoff is None or otoff is None:
1140 if allow_mixed: 1145 if allow_mixed:
1141 return 2 # arbitrary non-zero value 1146 return 2 # arbitrary non-zero value
1142 else: 1147 else:
1143 raise TypeError("cannot compare naive and aware times") 1148 raise TypeError("cannot compare naive and aware times")
1149
1144 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) 1150 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1)
1145 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) 1151 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1)
1152
1146 return _cmp((myhhmm, self._second, self._microsecond), 1153 return _cmp((myhhmm, self._second, self._microsecond),
1147 (othhmm, other._second, other._microsecond)) 1154 (othhmm, other._second, other._microsecond))
1148 1155
1149 def __hash__(self): 1156 def __hash__(self):
1150 """Hash.""" 1157 """Hash."""
1151 tzoff = self.utcoffset() 1158 tzoff = self.utcoffset()
1152 if not tzoff: # zero or None 1159 if not tzoff: # zero or None
1153 return hash(self._getstate()[0]) 1160 return hash(self._getstate()[0])
1154 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, 1161 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
1155 timedelta(hours=1)) 1162 timedelta(hours=1))
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
2157 _check_time_fields, _check_tzinfo_arg, _check_tzname, 2164 _check_time_fields, _check_tzinfo_arg, _check_tzname,
2158 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, 2165 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month,
2159 _days_before_year, _days_in_month, _format_time, _is_leap, 2166 _days_before_year, _days_in_month, _format_time, _is_leap,
2160 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, 2167 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class,
2161 _wrap_strftime, _ymd2ord) 2168 _wrap_strftime, _ymd2ord)
2162 # XXX Since import * above excludes names that start with _, 2169 # XXX Since import * above excludes names that start with _,
2163 # docstring does not get overwritten. In the future, it may be 2170 # docstring does not get overwritten. In the future, it may be
2164 # appropriate to maintain a single module level docstring and 2171 # appropriate to maintain a single module level docstring and
2165 # remove the following line. 2172 # remove the following line.
2166 from _datetime import __doc__ 2173 from _datetime import __doc__
LEFTRIGHT

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