Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 Loading... | |
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 | 1075 |
1076 if delta.days <= _MAXORDINAL: | 1076 return time(hour, minute, second, |
Ronald Oussoren
2013/02/25 08:35:59
The overflow test is imho not necessary
| |
1077 return time(hour, minute, second, | 1077 delta.microseconds, |
1078 delta.microseconds, | 1078 tzinfo=self._tzinfo) |
1079 tzinfo=self._tzinfo) | |
1080 raise OverflowError("result out of range") | |
1081 | 1079 |
1082 def __sub__(self, other): | 1080 def __sub__(self, other): |
1083 "Subtract a time and a timedelta." | 1081 "Subtract a time and a timedelta." |
1084 if isinstance(other, timedelta): | 1082 if isinstance(other, timedelta): |
1085 return self + -other | 1083 return self + -other |
Martin Panter
2019/04/14 02:28:55
This should be smarter to avoid OverflowError on s
| |
1086 return NotImplemented | 1084 return NotImplemented |
1087 | 1085 |
1088 # Standard conversions, __hash__ (and helpers) | 1086 # Standard conversions, __hash__ (and helpers) |
1089 | 1087 |
1090 # Comparisons of time objects with other. | 1088 # Comparisons of time objects with other. |
1091 | 1089 |
1092 def __eq__(self, other): | 1090 def __eq__(self, other): |
1093 if isinstance(other, time): | 1091 if isinstance(other, time): |
1094 return self._cmp(other, allow_mixed=True) == 0 | 1092 return self._cmp(other, allow_mixed=True) == 0 |
1095 else: | 1093 else: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1141 if base_compare: | 1139 if base_compare: |
1142 return _cmp((self._hour, self._minute, self._second, | 1140 return _cmp((self._hour, self._minute, self._second, |
1143 self._microsecond), | 1141 self._microsecond), |
1144 (other._hour, other._minute, other._second, | 1142 (other._hour, other._minute, other._second, |
1145 other._microsecond)) | 1143 other._microsecond)) |
1146 if myoff is None or otoff is None: | 1144 if myoff is None or otoff is None: |
1147 if allow_mixed: | 1145 if allow_mixed: |
1148 return 2 # arbitrary non-zero value | 1146 return 2 # arbitrary non-zero value |
1149 else: | 1147 else: |
1150 raise TypeError("cannot compare naive and aware times") | 1148 raise TypeError("cannot compare naive and aware times") |
1151 | 1149 |
Ronald Oussoren
2013/02/25 08:35:59
Spurious whitespace change
| |
1152 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) | 1150 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) |
1153 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) | 1151 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) |
1154 | 1152 |
1155 return _cmp((myhhmm, self._second, self._microsecond), | 1153 return _cmp((myhhmm, self._second, self._microsecond), |
1156 (othhmm, other._second, other._microsecond)) | 1154 (othhmm, other._second, other._microsecond)) |
1157 | 1155 |
1158 def __hash__(self): | 1156 def __hash__(self): |
1159 """Hash.""" | 1157 """Hash.""" |
1160 tzoff = self.utcoffset() | 1158 tzoff = self.utcoffset() |
1161 if not tzoff: # zero or None | 1159 if not tzoff: # zero or None |
(...skipping 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2166 _check_time_fields, _check_tzinfo_arg, _check_tzname, | 2164 _check_time_fields, _check_tzinfo_arg, _check_tzname, |
2167 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, | 2165 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, |
2168 _days_before_year, _days_in_month, _format_time, _is_leap, | 2166 _days_before_year, _days_in_month, _format_time, _is_leap, |
2169 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, | 2167 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, |
2170 _wrap_strftime, _ymd2ord) | 2168 _wrap_strftime, _ymd2ord) |
2171 # XXX Since import * above excludes names that start with _, | 2169 # XXX Since import * above excludes names that start with _, |
2172 # docstring does not get overwritten. In the future, it may be | 2170 # docstring does not get overwritten. In the future, it may be |
2173 # appropriate to maintain a single module level docstring and | 2171 # appropriate to maintain a single module level docstring and |
2174 # remove the following line. | 2172 # remove the following line. |
2175 from _datetime import __doc__ | 2173 from _datetime import __doc__ |
LEFT | RIGHT |