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

Delta Between Two Patch Sets: Lib/datetime.py

Issue 17267: datetime.time support for '+' and 'now'
Left Patch Set: Created 6 years, 2 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:
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
(no file at all)
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 1041 matching lines...) Expand 10 before | Expand all | Expand 10 after
1052 @property 1052 @property
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):
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):
1065 return NotImplemented
1066
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,
1069 seconds=self._second,
1070 microseconds=self._microsecond)
1071
1072 delta += other
1073 hour, rem = divmod(delta.seconds, 3600)
1074 minute, second = divmod(rem, 60)
1075
1076 return time(hour, minute, second,
1077 delta.microseconds,
1078 tzinfo=self._tzinfo)
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
1085
1062 # Standard conversions, __hash__ (and helpers) 1086 # Standard conversions, __hash__ (and helpers)
1063 1087
1064 # Comparisons of time objects with other. 1088 # Comparisons of time objects with other.
1065 1089
1066 def __eq__(self, other): 1090 def __eq__(self, other):
1067 if isinstance(other, time): 1091 if isinstance(other, time):
1068 return self._cmp(other, allow_mixed=True) == 0 1092 return self._cmp(other, allow_mixed=True) == 0
1069 else: 1093 else:
1070 return False 1094 return False
1071 1095
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 if base_compare: 1139 if base_compare:
1116 return _cmp((self._hour, self._minute, self._second, 1140 return _cmp((self._hour, self._minute, self._second,
1117 self._microsecond), 1141 self._microsecond),
1118 (other._hour, other._minute, other._second, 1142 (other._hour, other._minute, other._second,
1119 other._microsecond)) 1143 other._microsecond))
1120 if myoff is None or otoff is None: 1144 if myoff is None or otoff is None:
1121 if allow_mixed: 1145 if allow_mixed:
1122 return 2 # arbitrary non-zero value 1146 return 2 # arbitrary non-zero value
1123 else: 1147 else:
1124 raise TypeError("cannot compare naive and aware times") 1148 raise TypeError("cannot compare naive and aware times")
1149
1125 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) 1150 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1)
1126 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) 1151 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1)
1152
1127 return _cmp((myhhmm, self._second, self._microsecond), 1153 return _cmp((myhhmm, self._second, self._microsecond),
1128 (othhmm, other._second, other._microsecond)) 1154 (othhmm, other._second, other._microsecond))
1129 1155
1130 def __hash__(self): 1156 def __hash__(self):
1131 """Hash.""" 1157 """Hash."""
1132 tzoff = self.utcoffset() 1158 tzoff = self.utcoffset()
1133 if not tzoff: # zero or None 1159 if not tzoff: # zero or None
1134 return hash(self._getstate()[0]) 1160 return hash(self._getstate()[0])
1135 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, 1161 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
1136 timedelta(hours=1)) 1162 timedelta(hours=1))
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
2138 _check_time_fields, _check_tzinfo_arg, _check_tzname, 2164 _check_time_fields, _check_tzinfo_arg, _check_tzname,
2139 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, 2165 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month,
2140 _days_before_year, _days_in_month, _format_time, _is_leap, 2166 _days_before_year, _days_in_month, _format_time, _is_leap,
2141 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, 2167 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class,
2142 _wrap_strftime, _ymd2ord) 2168 _wrap_strftime, _ymd2ord)
2143 # XXX Since import * above excludes names that start with _, 2169 # XXX Since import * above excludes names that start with _,
2144 # docstring does not get overwritten. In the future, it may be 2170 # docstring does not get overwritten. In the future, it may be
2145 # appropriate to maintain a single module level docstring and 2171 # appropriate to maintain a single module level docstring and
2146 # remove the following line. 2172 # remove the following line.
2147 from _datetime import __doc__ 2173 from _datetime import __doc__
LEFTRIGHT

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