Index: Doc/lib/libdatetime.tex =================================================================== --- Doc/lib/libdatetime.tex (Revision 54211) +++ Doc/lib/libdatetime.tex (Arbeitskopie) @@ -951,6 +951,13 @@ \end{verbatim} \end{methoddesc} +\begin{methoddesc}{mimeformat}{} + Return a string representing the date and time in RFC 2822 format. + The member \member{tzinfo} must not be \code{None}. + + \versionadded{2.6} +\end{methoddesc} + \begin{methoddesc}{__str__}{} For a \class{datetime} instance \var{d}, \code{str(\var{d})} is equivalent to \code{\var{d}.isoformat(' ')}. Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (Revision 54211) +++ Lib/test/test_datetime.py (Arbeitskopie) @@ -28,7 +28,35 @@ OTHERSTUFF = (10, 10L, 34.5, "abc", {}, [], ()) +class UTC(tzinfo): + def utcoffset(self, dt): + return timedelta(0) + + def dst(self, dt): + return timedelta(0) + + def tzname(self, dt): + return "UTC" + +utc = UTC() + + +class UTC1(tzinfo): + def utcoffset(self, dt): + return timedelta(minutes=60) + + def dst(self, dt): + return timedelta(minutes=60) + + def tzname(self, dt): + return "UTC+0100" + +utc1 = UTC1() + + ############################################################################# + + # module tests class TestModule(unittest.TestCase): @@ -1593,6 +1621,10 @@ self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month + dt1.second - 7) + def test_mimeformat(self): + t = datetime(2007, 3, 6, 23, 1, 53) + self.assertRaises(TypeError, t.mimeformat) + class SubclassTime(time): sub_var = 1 @@ -2893,6 +2925,12 @@ self.assertEqual(dt1.utcoffset(), dt2.utcoffset()) self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.year - 7) + def test_mimeformat(self): + t = datetime(2007, 3, 6, 23, 1, 53, tzinfo=utc) + self.assertEqual(t.mimeformat(), "Tue, 06 Mar 2007 23:01:53 GMT") + t = datetime(2007, 3, 6, 23, 1, 53, tzinfo=utc1) + self.assertEqual(t.mimeformat(), "Tue, 06 Mar 2007 22:01:53 GMT") + # Pain to set up DST-aware tzinfo classes. def first_sunday_on_or_after(dt): Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (Revision 54211) +++ Modules/datetimemodule.c (Arbeitskopie) @@ -1070,17 +1070,17 @@ * String format helpers. */ +static const char *DayNames[] = { + "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" +}; +static const char *MonthNames[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +}; + static PyObject * format_ctime(PyDateTime_Date *date, int hours, int minutes, int seconds) { - static const char *DayNames[] = { - "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" - }; - static const char *MonthNames[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" - }; - char buffer[128]; int wday = weekday(GET_YEAR(date), GET_MONTH(date), GET_DAY(date)); @@ -4119,6 +4119,45 @@ } static PyObject * +datetime_mimeformat(PyDateTime_DateTime *self) +{ + char buffer[100]; + + int year = GET_YEAR(self); + int month = GET_MONTH(self); + int day = GET_DAY(self); + int hour = DATE_GET_HOUR(self); + int minute = DATE_GET_MINUTE(self); + int second = DATE_GET_SECOND(self); + int wday = weekday(year, month, day); + int microsecond = 0; + int offset; + int none; + + if (HASTZINFO(self)) { + offset = call_utcoffset(self->tzinfo, (PyObject *)self, &none); + if (offset == -1 && PyErr_Occurred()) + return NULL; + minute -= offset; + } + else { + PyErr_SetString(PyExc_TypeError, + "mimeformat() needs offset-aware datetime object"); + return NULL; + } + + if (normalize_datetime(&year, &month, &day, + &hour, &minute, &second, µsecond) < 0) + return NULL; + + PyOS_snprintf(buffer, sizeof(buffer), "%s, %02d %3s %4d %02d:%02d:%02d GMT", + DayNames[wday], day, MonthNames[month-1], year, + hour, minute, second); + + return PyString_FromString(buffer); +} + +static PyObject * datetime_ctime(PyDateTime_DateTime *self) { return format_ctime((PyDateTime_Date *)self, @@ -4517,6 +4556,10 @@ "sep is used to separate the year from the time, and " "defaults to 'T'.")}, + {"mimeformat", (PyCFunction)datetime_mimeformat, METH_NOARGS, + PyDoc_STR("-> string in RFC 2822 format, " + "e.g. Thu, 21 Dec 2006 16:01:25 GMT")}, + {"utcoffset", (PyCFunction)datetime_utcoffset, METH_NOARGS, PyDoc_STR("Return self.tzinfo.utcoffset(self).")},