Commit baf853c5 authored by Walter Dörwald's avatar Walter Dörwald

Change timedelta.__str__() to return a unicode object.

parent bafa137c
...@@ -1987,47 +1987,30 @@ delta_repr(PyDateTime_Delta *self) ...@@ -1987,47 +1987,30 @@ delta_repr(PyDateTime_Delta *self)
static PyObject * static PyObject *
delta_str(PyDateTime_Delta *self) delta_str(PyDateTime_Delta *self)
{ {
int days = GET_TD_DAYS(self);
int seconds = GET_TD_SECONDS(self);
int us = GET_TD_MICROSECONDS(self); int us = GET_TD_MICROSECONDS(self);
int hours; int seconds = GET_TD_SECONDS(self);
int minutes; int minutes = divmod(seconds, 60, &seconds);
char buf[100]; int hours = divmod(minutes, 60, &minutes);
char *pbuf = buf; int days = GET_TD_DAYS(self);
size_t buflen = sizeof(buf);
int n;
minutes = divmod(seconds, 60, &seconds);
hours = divmod(minutes, 60, &minutes);
if (days) { if (days) {
n = PyOS_snprintf(pbuf, buflen, "%d day%s, ", days, if (us)
(days == 1 || days == -1) ? "" : "s"); return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d.%06d",
if (n < 0 || (size_t)n >= buflen) days, (days == 1 || days == -1) ? "" : "s",
goto Fail; hours, minutes, seconds, us);
pbuf += n; else
buflen -= (size_t)n; return PyUnicode_FromFormat("%d day%s, %d:%02d:%02d",
} days, (days == 1 || days == -1) ? "" : "s",
hours, minutes, seconds);
n = PyOS_snprintf(pbuf, buflen, "%d:%02d:%02d", } else {
if (us)
return PyUnicode_FromFormat("%d:%02d:%02d.%06d",
hours, minutes, seconds, us);
else
return PyUnicode_FromFormat("%d:%02d:%02d",
hours, minutes, seconds); hours, minutes, seconds);
if (n < 0 || (size_t)n >= buflen)
goto Fail;
pbuf += n;
buflen -= (size_t)n;
if (us) {
n = PyOS_snprintf(pbuf, buflen, ".%06d", us);
if (n < 0 || (size_t)n >= buflen)
goto Fail;
pbuf += n;
} }
return PyString_FromStringAndSize(buf, pbuf - buf);
Fail:
PyErr_SetString(PyExc_SystemError, "goofy result from PyOS_snprintf");
return NULL;
} }
/* Pickle support, a simple use of __reduce__. */ /* Pickle support, a simple use of __reduce__. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment