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

Side by Side Diff: Lib/datetime.py

Issue 17267: datetime.time support for '+' and 'now'
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:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | Modules/_datetimemodule.c » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after
1051 1051
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
1062 def __add__(self, other):
1063 "Add a time and a timedelta"
1064 if not isinstance(other, timedelta):
1065 return NotImplemented
1066
1067 delta = timedelta(hours=self._hour,
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 if delta.days <= _MAXORDINAL:
Ronald Oussoren 2013/02/25 08:35:59 The overflow test is imho not necessary
1077 return time(hour, minute, second,
1078 delta.microseconds,
1079 tzinfo=self._tzinfo)
1080 raise OverflowError("result out of range")
1081
1082 def __sub__(self, other):
1083 "Subtract a time and a timedelta."
1084 if isinstance(other, timedelta):
1085 return self + -other
1086 return NotImplemented
1061 1087
1062 # Standard conversions, __hash__ (and helpers) 1088 # Standard conversions, __hash__ (and helpers)
1063 1089
1064 # Comparisons of time objects with other. 1090 # Comparisons of time objects with other.
1065 1091
1066 def __eq__(self, other): 1092 def __eq__(self, other):
1067 if isinstance(other, time): 1093 if isinstance(other, time):
1068 return self._cmp(other, allow_mixed=True) == 0 1094 return self._cmp(other, allow_mixed=True) == 0
1069 else: 1095 else:
1070 return False 1096 return False
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 if base_compare: 1141 if base_compare:
1116 return _cmp((self._hour, self._minute, self._second, 1142 return _cmp((self._hour, self._minute, self._second,
1117 self._microsecond), 1143 self._microsecond),
1118 (other._hour, other._minute, other._second, 1144 (other._hour, other._minute, other._second,
1119 other._microsecond)) 1145 other._microsecond))
1120 if myoff is None or otoff is None: 1146 if myoff is None or otoff is None:
1121 if allow_mixed: 1147 if allow_mixed:
1122 return 2 # arbitrary non-zero value 1148 return 2 # arbitrary non-zero value
1123 else: 1149 else:
1124 raise TypeError("cannot compare naive and aware times") 1150 raise TypeError("cannot compare naive and aware times")
1151
Ronald Oussoren 2013/02/25 08:35:59 Spurious whitespace change
1125 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1) 1152 myhhmm = self._hour * 60 + self._minute - myoff//timedelta(minutes=1)
1126 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1) 1153 othhmm = other._hour * 60 + other._minute - otoff//timedelta(minutes=1)
1154
1127 return _cmp((myhhmm, self._second, self._microsecond), 1155 return _cmp((myhhmm, self._second, self._microsecond),
1128 (othhmm, other._second, other._microsecond)) 1156 (othhmm, other._second, other._microsecond))
1129 1157
1130 def __hash__(self): 1158 def __hash__(self):
1131 """Hash.""" 1159 """Hash."""
1132 tzoff = self.utcoffset() 1160 tzoff = self.utcoffset()
1133 if not tzoff: # zero or None 1161 if not tzoff: # zero or None
1134 return hash(self._getstate()[0]) 1162 return hash(self._getstate()[0])
1135 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff, 1163 h, m = divmod(timedelta(hours=self.hour, minutes=self.minute) - tzoff,
1136 timedelta(hours=1)) 1164 timedelta(hours=1))
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
2138 _check_time_fields, _check_tzinfo_arg, _check_tzname, 2166 _check_time_fields, _check_tzinfo_arg, _check_tzname,
2139 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month, 2167 _check_utc_offset, _cmp, _cmperror, _date_class, _days_before_month,
2140 _days_before_year, _days_in_month, _format_time, _is_leap, 2168 _days_before_year, _days_in_month, _format_time, _is_leap,
2141 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class, 2169 _isoweek1monday, _math, _ord2ymd, _time, _time_class, _tzinfo_class,
2142 _wrap_strftime, _ymd2ord) 2170 _wrap_strftime, _ymd2ord)
2143 # XXX Since import * above excludes names that start with _, 2171 # XXX Since import * above excludes names that start with _,
2144 # docstring does not get overwritten. In the future, it may be 2172 # docstring does not get overwritten. In the future, it may be
2145 # appropriate to maintain a single module level docstring and 2173 # appropriate to maintain a single module level docstring and
2146 # remove the following line. 2174 # remove the following line.
2147 from _datetime import __doc__ 2175 from _datetime import __doc__
OLDNEW
« no previous file with comments | « no previous file | Lib/test/datetimetester.py » ('j') | Modules/_datetimemodule.c » ('J')

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