[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.c,1.5,1.6
Fred L. Drake
fdrake@users.sourceforge.net
Tue, 05 Mar 2002 21:53:36 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv15873
Modified Files:
datetime.c
Log Message:
Added implementation of isocalendar().
Index: datetime.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** datetime.c 6 Mar 2002 04:30:51 -0000 1.5
--- datetime.c 6 Mar 2002 05:53:34 -0000 1.6
***************
*** 96,99 ****
--- 96,113 ----
}
+ static int
+ iso_week1_monday(int year)
+ {
+ int first_day = ymd_to_ord(year, 1, 1);
+ int first_weekday = (first_day + 6) % 7;
+ int week1_monday = first_day - first_weekday;
+
+ #define THURSDAY 3
+ if (first_weekday > THURSDAY)
+ week1_monday += 7;
+ #undef THURSDAY
+ return week1_monday;
+ }
+
/*
***************
*** 249,252 ****
--- 263,290 ----
static PyObject *
+ datetime_isocalendar(PyDateTime_Object *self)
+ {
+ int year = GET_YEAR(self);
+ int week1_monday = iso_week1_monday(year);
+ long today = ymd_to_ord(year, GET_MONTH(self), GET_DAY(self));
+ int week = (today - week1_monday) / 7;
+ int day = (today - week1_monday) % 7;
+
+ if (week < 0) {
+ --year;
+ week1_monday = iso_week1_monday(year);
+ week = (today - week1_monday) / 7;
+ day = (today - week1_monday) % 7;
+ }
+ else if (week >= 52 &&
+ today >= iso_week1_monday(year + 1)) {
+ ++year;
+ week = 0;
+ }
+ return Py_BuildValue("iii", year, week + 1, day + 1);
+ }
+
+
+ static PyObject *
datetime_isoformat(PyDateTime_Object *self, PyObject *args, PyObject *kw)
{
***************
*** 285,295 ****
static PyMethodDef datetime_methods[] = {
! {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS|METH_KEYWORDS,
"Return the day of the week represented by the datetime.\n"
"Monday == 1 ... Sunday == 7"},
! {"isoweekday", (PyCFunction)datetime_isoweekday, METH_NOARGS,
"Return the day of the week represented by the datetime.\n"
"Monday == 1 ... Sunday == 7"},
! {"weekday", (PyCFunction)datetime_weekday, METH_NOARGS,
"Return the day of the week represented by the datetime.\n"
"Monday == 0 ... Sunday == 6"},
--- 323,337 ----
static PyMethodDef datetime_methods[] = {
! {"isocalendar", (PyCFunction)datetime_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 rest derives from that."},
! {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS|METH_KEYWORDS,
"Return the day of the week represented by the datetime.\n"
"Monday == 1 ... Sunday == 7"},
! {"isoweekday", (PyCFunction)datetime_isoweekday, METH_NOARGS,
"Return the day of the week represented by the datetime.\n"
"Monday == 1 ... Sunday == 7"},
! {"weekday", (PyCFunction)datetime_weekday, METH_NOARGS,
"Return the day of the week represented by the datetime.\n"
"Monday == 0 ... Sunday == 6"},