[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.41,1.42 doc.txt,1.9,1.10 obj_date.c,1.25,1.26 test_both.py,1.35,1.36

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
2002年12月04日 21:04:12 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv25346
Modified Files:
	datetime.c doc.txt obj_date.c test_both.py 
Log Message:
Implemented, documented, and added a test for date.strftime().
Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** datetime.c	5 Dec 2002 04:47:16 -0000	1.41
--- datetime.c	5 Dec 2002 05:04:09 -0000	1.42
***************
*** 345,355 ****
 	assert(PyTuple_Size(tuple) == 9);
 
- 	if (! PyString_Check(format)) {
- 		PyErr_Format(PyExc_TypeError,
- 			 "strftime requires a string format, not "
- 			 "type '%s'", format->ob_type->tp_name);
- 		return NULL;
- 	}
- 
 	time = PyImport_ImportModule("time");
 	if (time == NULL)
--- 345,348 ----
***************
*** 361,367 ****
 		return NULL;
 
! 	result = PyObject_CallFunction(time_strftime, "OO",
! 				 	format,
! 				 	tuple);
 	Py_DECREF(time_strftime);
 	return result;
--- 354,358 ----
 		return NULL;
 
! 	result = PyObject_CallFunction(time_strftime, "OO", format, tuple);
 	Py_DECREF(time_strftime);
 	return result;
Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** doc.txt	5 Dec 2002 04:47:16 -0000	1.9
--- doc.txt	5 Dec 2002 05:04:09 -0000	1.10
***************
*** 299,304 ****
 
 - strftime(format)
! XXX And the C implementation doesn't have this yet either.
! XXX And there are no tests for it.
 
 
--- 299,305 ----
 
 - strftime(format)
! Return a string representing the date, controlled by an explicit
! format string. d.strftime(f) is the same as
! time.strftime(f, d.timetuple()).
 
 
Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -d -r1.25 -r1.26
*** obj_date.c	5 Dec 2002 04:49:42 -0000	1.25
--- obj_date.c	5 Dec 2002 05:04:09 -0000	1.26
***************
*** 351,354 ****
--- 351,367 ----
 }
 
+ static PyObject *
+ date_strftime(PyDateTime_Date *self, PyObject *format)
+ {
+ 	PyObject *result;
+ 	PyObject *tuple = date_timetuple(self);
+ 
+ 	if (tuple == NULL)
+ 		return NULL;
+ 	result = format_strftime(format, tuple);
+ 	Py_DECREF(tuple);
+ 	return result;
+ }
+ 
 /* XXX This is much more complicated than date_add. Why? */
 static PyObject *
***************
*** 492,524 ****
--- 505,552 ----
 							 METH_CLASS,
 	 "timestamp -> local date from a POSIX timestamp (like time.time())."},
+ 
 	{"fromordinal", (PyCFunction)date_fromordinal,	METH_VARARGS |
 							METH_CLASS,
 	 "int -> date corresponding to a proleptic Gregorian ordinal."},
+ 
 	{"today", (PyCFunction)date_today,	METH_O | METH_CLASS,
 	 "Construct local date corresponing to current day."},
 
 	/* Instance methods: */
+ 
 	{"ctime", (PyCFunction)date_ctime, METH_NOARGS,
 	 "Return ctime() style string."},
+ 
+ 	{"strftime", 	(PyCFunction)date_strftime,	METH_O,
+ 	 "format -> strftime() style string."},
+ 
 	{"timetuple", (PyCFunction)date_timetuple, METH_NOARGS,
 "Return time tuple, compatible with time.localtime()."},
+ 
 	{"isocalendar", (PyCFunction)date_isocalendar, METH_NOARGS,
 	 "Return a 3-tuple containing ISO year, week number, and weekday.\n\n"
 	 "The first ISO week of the year is the (Mon-Sun) week containing the\n"
 	 "year's first Thursday; everything else derives from that."},
+ 
 	{"isoformat", (PyCFunction)date_str, 	METH_NOARGS,
 	 "Return string in ISO 8601 format, YYYY-MM-DD."},
+ 
 	{"isoweekday", (PyCFunction)date_isoweekday, METH_NOARGS,
 	 "Return the day of the week represented by the date.\n"
 	 "Monday == 1 ... Sunday == 7"},
+ 
 	{"toordinal", (PyCFunction)date_toordinal, METH_NOARGS,
 	 "Return proleptic Gregorian ordinal. January 1 of year 1 is day 1."},
+ 
 	{"weekday", (PyCFunction)date_weekday, METH_NOARGS,
 	 "Return the day of the week represented by the date.\n"
 	 "Monday == 0 ... Sunday == 6"},
+ 
 	{"__setstate__", (PyCFunction)date_setstate,	METH_O,
 	 	PyDoc_STR("__setstate__(state)")},
+ 
 	{"__getstate__", (PyCFunction)date_getstate,	METH_NOARGS,
 	 	PyDoc_STR("__getstate__() -> state")},
+ 
 	{NULL,	NULL}
 };
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** test_both.py	4 Dec 2002 23:08:47 -0000	1.35
--- test_both.py	5 Dec 2002 05:04:09 -0000	1.36
***************
*** 572,575 ****
--- 572,583 ----
 self.assertEqual(t.ctime(), "Sat Mar 2 00:00:00 2002")
 
+ def test_strftime(self):
+ t = self.theclass(2005, 3, 2)
+ self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05")
+ 
+ self.assertRaises(TypeError, t.strftime) # needs an arg
+ self.assertRaises(TypeError, t.strftime, "one", "two") # too many args
+ self.assertRaises(TypeError, t.strftime, 42) # arg wrong type
+ 
 def test_resolution_info(self):
 self.assert_(isinstance(self.theclass.min, self.theclass))

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