[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.75,1.76 obj_date.c,1.26,1.27

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月05日 09:07:23 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv3763
Modified Files:
	datetime.py obj_date.c 
Log Message:
No semantic change. Rearranged some code and changed comments, trying
to sort out what the intent of date +/- timedelta is. The Python and
C implementations disagree on whether this should or should not ever
return a datetime.
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.75
retrieving revision 1.76
diff -C2 -d -r1.75 -r1.76
*** datetime.py	5 Dec 2002 01:39:39 -0000	1.75
--- datetime.py	5 Dec 2002 17:07:20 -0000	1.76
***************
*** 636,646 ****
 __radd__ = __add__
 
 timedelta_class = timedelta # Allows a subclass to override
 
 def __sub__(self, other):
! """Subtract two dates, or a date and a timedelta.
! 
! An int/long/float argument is also allowed, interpreted as seconds.
! """
 if isinstance(other, timedelta):
 return self + -other
--- 636,647 ----
 __radd__ = __add__
 
+ # XXX What is timedelta_class for? If a subclass wants to redefine
+ # XXX subtraction, it should redefine subtraction.
+ # XXX The C implementation has nothing like this, and there's
+ # XXX no test for it.
 timedelta_class = timedelta # Allows a subclass to override
 
 def __sub__(self, other):
! """Subtract two dates, or a date and a timedelta."""
 if isinstance(other, timedelta):
 return self + -other
***************
*** 651,659 ****
 return NotImplemented
 
- # Day-of-the-week and week-of-the-year, according to ISO
- 
 def weekday(self):
 "Return day of the week, where Monday == 0 ... Sunday == 6."
 return (self.toordinal() + 6) % 7
 
 def isoweekday(self):
--- 652,660 ----
 return NotImplemented
 
 def weekday(self):
 "Return day of the week, where Monday == 0 ... Sunday == 6."
 return (self.toordinal() + 6) % 7
+ 
+ # Day-of-the-week and week-of-the-year, according to ISO
 
 def isoweekday(self):
Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** obj_date.c	5 Dec 2002 05:04:09 -0000	1.26
--- obj_date.c	5 Dec 2002 17:07:20 -0000	1.27
***************
*** 29,35 ****
 datetime_subtract(PyObject *left, PyObject *right);
 
- /* date(2009, 12, 28) + timedelta(4) == 2010年01月01日 (2010, 1, -2)
- expected (2009, 53, 5)
- */
 static PyObject *
 add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta)
--- 29,32 ----
***************
*** 94,97 ****
--- 91,159 ----
 }
 
+ static PyObject *
+ date_subtract(PyObject *left, PyObject *right)
+ {
+ 	PyTypeObject *left_type = left->ob_type;
+ 	PyTypeObject *right_type = right->ob_type;
+ 
+ 	if (PyType_IsSubtype(left_type, &PyDateTime_DateTimeType)
+ 	 || PyType_IsSubtype(right_type, &PyDateTime_DateTimeType)) {
+ 		Py_INCREF(Py_NotImplemented);
+ 		return Py_NotImplemented;
+ 	}
+ 	if (PyType_IsSubtype(left_type, &PyDateTime_DateType)) {
+ 		if (PyType_IsSubtype(right_type, &PyDateTime_DateType)) {
+ 			/* date - date */
+ 			long left_ord = ymd_to_ord(GET_YEAR(left),
+ 						 GET_MONTH(left),
+ 						 GET_DAY(left));
+ 			long right_ord = ymd_to_ord(GET_YEAR(right),
+ 						 GET_MONTH(right),
+ 						 GET_DAY(right));
+ 			return new_delta(left_ord - right_ord, 0, 0, 0);
+ 		}
+ 		if (PyType_IsSubtype(right_type, &PyDateTime_DeltaType)) {
+ 			PyObject *result = NULL;
+ 			if (GET_TD_SECONDS(right) != 0
+ 			 || GET_TD_MICROSECONDS(right) != 0) {
+ 				/* need to convert to datetime + delta */
+ 				PyObject *dt = new_datetime(GET_YEAR(left),
+ 							 GET_MONTH(left),
+ 							 GET_DAY(left),
+ 							 0, 0, 0, 0);
+ 				if (dt != NULL) {
+ 					result = datetime_subtract(dt, right);
+ 					Py_DECREF(dt);
+ 				}
+ 			}
+ 			else if (GET_TD_DAYS(right) == 0) {
+ 				/* date - timedelta(0) */
+ 				Py_INCREF(left);
+ 				result = left;
+ 			}
+ 			else {
+ 				long year, month, day;
+ 				long ord = ymd_to_ord(GET_YEAR(left),
+ 						 GET_MONTH(left),
+ 						 GET_DAY(left));
+ 				ord -= GET_TD_DAYS(right);
+ 				if (ord < 1)
+ 					goto Overflow;
+ 				ord_to_ymd(ord, &year, &month, &day);
+ 				if (year < MINYEAR || year > MAXYEAR)
+ 					goto Overflow;
+ 				result = new_date(year, month, day);
+ 			}
+ 			return result;
+ 		}
+ 	}
+ 	Py_INCREF(Py_NotImplemented);
+ 	return Py_NotImplemented;
+ 
+ Overflow:
+ 	PyErr_SetString(PyExc_OverflowError, "date subtraction");
+ 	return NULL;
+ }
+ 
 /* This is more natural as a tp_compare, but doesn't work then: for whatever
 * reason, Python's try_3way_compare ignores tp_compare unless
***************
*** 362,431 ****
 	Py_DECREF(tuple);
 	return result;
- }
- 
- /* XXX This is much more complicated than date_add. Why? */
- static PyObject *
- date_subtract(PyObject *left, PyObject *right)
- {
- 	PyTypeObject *left_type = left->ob_type;
- 	PyTypeObject *right_type = right->ob_type;
- 
- 	if (PyType_IsSubtype(left_type, &PyDateTime_DateTimeType)
- 	 || PyType_IsSubtype(right_type, &PyDateTime_DateTimeType)) {
- 		Py_INCREF(Py_NotImplemented);
- 		return Py_NotImplemented;
- 	}
- 	if (PyType_IsSubtype(left_type, &PyDateTime_DateType)) {
- 		if (PyType_IsSubtype(right_type, &PyDateTime_DateType)) {
- 			/* date - date */
- 			long left_ord = ymd_to_ord(GET_YEAR(left),
- 						 GET_MONTH(left),
- 						 GET_DAY(left));
- 			long right_ord = ymd_to_ord(GET_YEAR(right),
- 						 GET_MONTH(right),
- 						 GET_DAY(right));
- 			return new_delta(left_ord - right_ord, 0, 0, 0);
- 		}
- 		if (PyType_IsSubtype(right_type, &PyDateTime_DeltaType)) {
- 			PyObject *result = NULL;
- 			if (GET_TD_SECONDS(right) != 0
- 			 || GET_TD_MICROSECONDS(right) != 0) {
- 				/* need to convert to datetime + delta */
- 				PyObject *dt = new_datetime(GET_YEAR(left),
- 							 GET_MONTH(left),
- 							 GET_DAY(left),
- 							 0, 0, 0, 0);
- 				if (dt != NULL) {
- 					result = datetime_subtract(dt, right);
- 					Py_DECREF(dt);
- 				}
- 			}
- 			else if (GET_TD_DAYS(right) == 0) {
- 				/* date - timedelta(0) */
- 				Py_INCREF(left);
- 				result = left;
- 			}
- 			else {
- 				long year, month, day;
- 				long ord = ymd_to_ord(GET_YEAR(left),
- 						 GET_MONTH(left),
- 						 GET_DAY(left));
- 				ord -= GET_TD_DAYS(right);
- 				if (ord < 1)
- 					goto Overflow;
- 				ord_to_ymd(ord, &year, &month, &day);
- 				if (year < MINYEAR || year > MAXYEAR)
- 					goto Overflow;
- 				result = new_date(year, month, day);
- 			}
- 			return result;
- 		}
- 	}
- 	Py_INCREF(Py_NotImplemented);
- 	return Py_NotImplemented;
- 
- Overflow:
- 	PyErr_SetString(PyExc_OverflowError, "date subtraction");
- 	return NULL;
 }
 
--- 424,427 ----

AltStyle によって変換されたページ (->オリジナル) /