[Python-checkins] python/dist/src/Modules datetimemodule.c,1.13,1.14

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月23日 21:41:29 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv4868/python/Modules
Modified Files:
	datetimemodule.c 
Log Message:
Implemented .replace() methods for date, datetime, datetimetz, time and
timetz.
Index: datetimemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/datetimemodule.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** datetimemodule.c	23 Dec 2002 16:17:39 -0000	1.13
--- datetimemodule.c	24 Dec 2002 05:41:27 -0000	1.14
***************
*** 2144,2147 ****
--- 2144,2149 ----
 /* Constructors. */
 
+ static char *date_kws[] = {"year", "month", "day", NULL};
+ 
 static PyObject *
 date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 2152,2160 ****
 	int day;
 
! 	static char *keywords[] = {
! 		"year", "month", "day", NULL
! 	};
! 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii", keywords,
 					&year, &month, &day)) {
 		if (check_date_args(year, month, day) < 0)
--- 2154,2158 ----
 	int day;
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
 					&year, &month, &day)) {
 		if (check_date_args(year, month, day) < 0)
***************
*** 2455,2458 ****
--- 2453,2476 ----
 }
 
+ static PyObject *
+ date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *clone;
+ 	PyObject *tuple;
+ 	int year = GET_YEAR(self);
+ 	int month = GET_MONTH(self);
+ 	int day = GET_DAY(self);
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
+ 					 &year, &month, &day))
+ 		return NULL;
+ 	tuple = Py_BuildValue("iii", year, month, day);
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	clone = date_new(self->ob_type, tuple, NULL);
+ 	Py_DECREF(tuple);
+ 	return clone;
+ }
+ 
 static PyObject *date_getstate(PyDateTime_Date *self);
 
***************
*** 2603,2606 ****
--- 2621,2627 ----
 		 "Monday == 0 ... Sunday == 6")},
 
+ 	{"replace", (PyCFunction)date_replace, METH_KEYWORDS,
+ 	 PyDoc_STR("Return date with new specified fields.")},
+ 
 	{"__setstate__", (PyCFunction)date_setstate,	METH_O,
 	 PyDoc_STR("__setstate__(state)")},
***************
*** 2713,2716 ****
--- 2734,2742 ----
 /* Constructors. */
 
+ 
+ static char *datetime_kws[] = {"year", "month", "day",
+ 			 "hour", "minute", "second", "microsecond",
+ 			 NULL};
+ 
 static PyObject *
 datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 2725,2734 ****
 	int usecond = 0;
 
! 	static char *keywords[] = {
! 		"year", "month", "day", "hour", "minute", "second",
! 		"microsecond", NULL
! 	};
! 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiii", keywords,
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
--- 2751,2755 ----
 	int usecond = 0;
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiii", datetime_kws,
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond)) {
***************
*** 3202,3205 ****
--- 3223,3251 ----
 
 static PyObject *
+ datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *clone;
+ 	PyObject *tuple;
+ 	int y = GET_YEAR(self);
+ 	int m = GET_MONTH(self);
+ 	int d = GET_DAY(self);
+ 	int hh = DATE_GET_HOUR(self);
+ 	int mm = DATE_GET_MINUTE(self);
+ 	int ss = DATE_GET_SECOND(self);
+ 	int us = DATE_GET_MICROSECOND(self);
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiii:replace",
+ 					 datetime_kws,
+ 					 &y, &m, &d, &hh, &mm, &ss, &us))
+ 		return NULL;
+ 	tuple = Py_BuildValue("iiiiiii", y, m, d, hh, mm, ss, us);
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	clone = datetime_new(self->ob_type, tuple, NULL);
+ 	Py_DECREF(tuple);
+ 	return clone;
+ }
+ 
+ static PyObject *
 datetime_timetuple(PyDateTime_DateTime *self)
 {
***************
*** 3349,3352 ****
--- 3395,3401 ----
 	 	 "to 'T'.")},
 
+ 	{"replace", (PyCFunction)datetime_replace,	METH_KEYWORDS,
+ 	 PyDoc_STR("Return datetime with new specified fields.")},
+ 
 	{"__setstate__", (PyCFunction)datetime_setstate, METH_O,
 	 PyDoc_STR("__setstate__(state)")},
***************
*** 3458,3461 ****
--- 3507,3512 ----
 /* Constructors. */
 
+ static char *time_kws[] = {"hour", "minute", "second", "microsecond", NULL};
+ 
 static PyObject *
 time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 3467,3475 ****
 	int usecond = 0;
 
- 	static char *keywords[] = {
- 		"hour", "minute", "second", "microsecond", NULL
- 	};
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", keywords,
 					&hour, &minute, &second, &usecond)) {
 		if (check_time_args(hour, minute, second, usecond) < 0)
--- 3518,3523 ----
 	int usecond = 0;
 
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", time_kws,
 					&hour, &minute, &second, &usecond)) {
 		if (check_time_args(hour, minute, second, usecond) < 0)
***************
*** 3670,3673 ****
--- 3718,3743 ----
 }
 
+ static PyObject *
+ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *clone;
+ 	PyObject *tuple;
+ 	int hh = TIME_GET_HOUR(self);
+ 	int mm = TIME_GET_MINUTE(self);
+ 	int ss = TIME_GET_SECOND(self);
+ 	int us = TIME_GET_MICROSECOND(self);
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiii:replace",
+ 					 time_kws,
+ 					 &hh, &mm, &ss, &us))
+ 		return NULL;
+ 	tuple = Py_BuildValue("iiii", hh, mm, ss, us);
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	clone = time_new(self->ob_type, tuple, NULL);
+ 	Py_DECREF(tuple);
+ 	return clone;
+ }
+ 
 static int
 time_nonzero(PyDateTime_Time *self)
***************
*** 3760,3763 ****
--- 3830,3836 ----
 	 PyDoc_STR("format -> strftime() style string.")},
 
+ 	{"replace", (PyCFunction)time_replace,	METH_KEYWORDS,
+ 	 PyDoc_STR("Return datetime with new specified fields.")},
+ 
 	{"__setstate__", (PyCFunction)time_setstate,	METH_O,
 	 PyDoc_STR("__setstate__(state)")},
***************
*** 3978,3981 ****
--- 4051,4057 ----
 */
 
+ static char *timetz_kws[] = {"hour", "minute", "second", "microsecond",
+ 			 "tzinfo", NULL};
+ 
 static PyObject *
 timetz_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 3988,3996 ****
 	PyObject *tzinfo = Py_None;
 
! 	static char *keywords[] = {
! 		"hour", "minute", "second", "microsecond", "tzinfo", NULL
! 	};
! 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", keywords,
 					&hour, &minute, &second, &usecond,
 					&tzinfo)) {
--- 4064,4068 ----
 	PyObject *tzinfo = Py_None;
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", timetz_kws,
 					&hour, &minute, &second, &usecond,
 					&tzinfo)) {
***************
*** 4079,4082 ****
--- 4151,4177 ----
 /* Note: tp_richcompare and tp_hash are inherited from time. */
 
+ static PyObject *
+ timetz_replace(PyDateTime_TimeTZ *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *clone;
+ 	PyObject *tuple;
+ 	int hh = TIME_GET_HOUR(self);
+ 	int mm = TIME_GET_MINUTE(self);
+ 	int ss = TIME_GET_SECOND(self);
+ 	int us = TIME_GET_MICROSECOND(self);
+ 	PyObject *tzinfo = self->tzinfo;
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
+ 					 timetz_kws,
+ 					 &hh, &mm, &ss, &us, &tzinfo))
+ 		return NULL;
+ 	tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	clone = timetz_new(self->ob_type, tuple, NULL);
+ 	Py_DECREF(tuple);
+ 	return clone;
+ }
+ 
 static int
 timetz_nonzero(PyDateTime_TimeTZ *self)
***************
*** 4205,4208 ****
--- 4300,4306 ----
 	 PyDoc_STR("Return self.tzinfo.dst(self).")},
 
+ 	{"replace", (PyCFunction)timetz_replace,	METH_KEYWORDS,
+ 	 PyDoc_STR("Return timetz with new specified fields.")},
+ 
 	{"__setstate__", (PyCFunction)timetz_setstate,	METH_O,
 	 PyDoc_STR("__setstate__(state)")},
***************
*** 4315,4318 ****
--- 4413,4421 ----
 }
 
+ static char *datetimetz_kws[] = {
+ 	"year", "month", "day", "hour", "minute", "second",
+ 	"microsecond", "tzinfo", NULL
+ };
+ 
 static PyObject *
 datetimetz_new(PyTypeObject *type, PyObject *args, PyObject *kw)
***************
*** 4328,4337 ****
 	PyObject *tzinfo = Py_None;
 
! 	static char *keywords[] = {
! 		"year", "month", "day", "hour", "minute", "second",
! 		"microsecond", "tzinfo", NULL
! 	};
! 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", keywords,
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond, &tzinfo)) {
--- 4431,4435 ----
 	PyObject *tzinfo = Py_None;
 
! 	if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetimetz_kws,
 					&year, &month, &day, &hour, &minute,
 					&second, &usecond, &tzinfo)) {
***************
*** 4573,4576 ****
--- 4671,4701 ----
 
 static PyObject *
+ datetimetz_replace(PyDateTime_DateTimeTZ *self, PyObject *args, PyObject *kw)
+ {
+ 	PyObject *clone;
+ 	PyObject *tuple;
+ 	int y = GET_YEAR(self);
+ 	int m = GET_MONTH(self);
+ 	int d = GET_DAY(self);
+ 	int hh = DATE_GET_HOUR(self);
+ 	int mm = DATE_GET_MINUTE(self);
+ 	int ss = DATE_GET_SECOND(self);
+ 	int us = DATE_GET_MICROSECOND(self);
+ 	PyObject *tzinfo = self->tzinfo;
+ 
+ 	if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
+ 					 datetimetz_kws,
+ 					 &y, &m, &d, &hh, &mm, &ss, &us,
+ 					 &tzinfo))
+ 		return NULL;
+ 	tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	clone = datetimetz_new(self->ob_type, tuple, NULL);
+ 	Py_DECREF(tuple);
+ 	return clone;
+ }
+ 
+ static PyObject *
 datetimetz_timetuple(PyDateTime_DateTimeTZ *self)
 {
***************
*** 4780,4783 ****
--- 4905,4911 ----
 	{"dst",		(PyCFunction)datetimetz_dst, METH_NOARGS,
 	 PyDoc_STR("Return self.tzinfo.dst(self).")},
+ 
+ 	{"replace", (PyCFunction)datetimetz_replace,	METH_KEYWORDS,
+ 	 PyDoc_STR("Return datetimetz with new specified fields.")},
 
 	{"__setstate__", (PyCFunction)datetimetz_setstate, METH_O,

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