[Python-checkins] python/nondist/sandbox/datetime datetime.c,1.40,1.41 doc.txt,1.8,1.9 obj_date.c,1.23,1.24
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
2002年12月04日 20:47:18 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv20378
Modified Files:
datetime.c doc.txt obj_date.c
Log Message:
date_isoformat() was essentially identical to date_str(), so got rid of
the former. Noted in the docs that they do the same thing. Added
XXX comment pointing out an odd asymmetry between date_add() and
date_subtract(): the latter can return a datetime object, but the
former never can. Is it intended that adding a date to a timedelta
with a non-zero non-days member produce a datetime instead of a date?
If so, date_add() needs reworking; but if not, data_subtract() needs
reworking.
Got a start on implementing the sundry .strftime() methods.
Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** datetime.c 4 Dec 2002 05:08:14 -0000 1.40
--- datetime.c 5 Dec 2002 04:47:16 -0000 1.41
***************
*** 333,336 ****
--- 333,371 ----
}
+ /* I sure don't want to reproduce the strftime code from the time module,
+ * so this imports the module and calls it. Slow!
+ */
+ static PyObject *
+ format_strftime(PyObject *format, PyObject *tuple)
+ {
+ PyObject *time;
+ PyObject *time_strftime;
+ PyObject *result;
+
+ 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)
+ return NULL;
+
+ time_strftime = PyObject_GetAttrString(time, "strftime");
+ Py_DECREF(time);
+ if (time_strftime == NULL)
+ return NULL;
+
+ result = PyObject_CallFunction(time_strftime, "OO",
+ format,
+ tuple);
+ Py_DECREF(time_strftime);
+ return result;
+ }
+
static char *
isoformat_date(PyDateTime_Date *dt, char buffer[], int bufflen)
Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** doc.txt 5 Dec 2002 01:56:50 -0000 1.8
--- doc.txt 5 Dec 2002 04:47:16 -0000 1.9
***************
*** 291,294 ****
--- 291,295 ----
'YYYY-MM-DD'. For example,
date(2002, 12, 4).isoformat() == '2002-12-04'.
+ str(d) is equivalent to d.isoformat().
- ctime()
Index: obj_date.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/obj_date.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** obj_date.c 5 Dec 2002 01:39:39 -0000 1.23
--- obj_date.c 5 Dec 2002 04:47:16 -0000 1.24
***************
*** 313,326 ****
static PyObject *
- date_isoformat(PyDateTime_Date *self, PyObject *args, PyObject *kw)
- {
- char buffer[128];
-
- isoformat_date(self, buffer, sizeof(buffer));
-
- return PyString_FromString(buffer);
- }
-
- static PyObject *
date_isoweekday(PyDateTime_Date *self)
{
--- 313,316 ----
***************
*** 362,365 ****
--- 352,356 ----
}
+ /* XXX This is much more complicated than date_add. Why? */
static PyObject *
date_subtract(PyObject *left, PyObject *right)
***************
*** 509,528 ****
/* Instance methods: */
! {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
"Return ctime() 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_isoformat, METH_KEYWORDS,
"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"},
--- 500,519 ----
/* Instance methods: */
! {"ctime", (PyCFunction)date_ctime, METH_NOARGS,
"Return ctime() 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"},