diff -r bb7363b8b50e Doc/library/datetime.rst --- a/Doc/library/datetime.rst Fri Sep 11 11:31:07 2015 -0700 +++ b/Doc/library/datetime.rst Mon Oct 05 00:50:51 2015 +0530 @@ -1872,6 +1872,14 @@ | | or -HHMM (empty string if the | +1030 | | | | the object is naive). | | | +-----------+--------------------------------+------------------------+-------+ +| ``%:z`` | UTC offset in the form +HH:MM | (empty), +00:00, | | +| | or -HH:MM (empty string if the | -04:00, +10:30 | | +| | the object is naive). | | | ++-----------+--------------------------------+------------------------+-------+ +| ``%::z`` | UTC offset in the form | (empty), +00:00:00, | | +| | +HH:MM:SS or -HH:MM:SS (empty | -04:00:20, +10:30:37 | | +| | string if the object is naive).| | | ++-----------+--------------------------------+------------------------+-------+ | ``%Z`` | Time zone name (empty string | (empty), UTC, EST, CST | | | | if the object is naive). | | | +-----------+--------------------------------+------------------------+-------+ diff -r bb7363b8b50e Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Fri Sep 11 11:31:07 2015 -0700 +++ b/Modules/_datetimemodule.c Mon Oct 05 00:50:51 2015 +0530 @@ -1039,7 +1039,7 @@ */ static int format_utcoffset(char *buf, size_t buflen, const char *sep, - PyObject *tzinfo, PyObject *tzinfoarg) + PyObject *tzinfo, PyObject *tzinfoarg, PyObject *secondsrequired) { PyObject *offset; int hours, minutes, seconds; @@ -1072,9 +1072,14 @@ Py_DECREF(offset); minutes = divmod(seconds, 60, &seconds); hours = divmod(minutes, 60, &minutes); - assert(seconds == 0); + /* assert(seconds == 0) */ /* XXX ignore sub-minute data, curently not allowed. */ + if ((PyObject *)secondsrequired == Py_True){ + PyOS_snprintf(buf, buflen, "%c%02d%s%02d%s%02d", sign, hours, sep, minutes, sep, seconds); + } + else{ PyOS_snprintf(buf, buflen, "%c%02d%s%02d", sign, hours, sep, minutes); + } return 0; } @@ -1201,6 +1206,45 @@ goto Done; } /* A % has been seen and ch is the character after it. */ + else if (ch == ':') { + if (zreplacement == NULL) { + /* format utcoffset */ + char buf[100]; + PyObject *tzinfo = get_tzinfo_member(object); + zreplacement = PyBytes_FromStringAndSize("", 0); + if (zreplacement == NULL) goto Done; + if (tzinfo != Py_None && tzinfo != NULL) { + assert(tzinfoarg != NULL); + /* If the next character after ':' is 'z' return the timzone formatted as signHH:MM */ + if ((ch = *pin++) == 'z') { + if (format_utcoffset(buf, + sizeof(buf), + ":", + tzinfo, + tzinfoarg, Py_False) < 0) + goto Done; + } + if (ch == ':' && (ch == *pin++)!='\0' && ch == 'z'){ + if (format_utcoffset(buf, + sizeof(buf), + ":", + tzinfo, + tzinfoarg, Py_True) < 0) + goto Done; + + } + Py_DECREF(zreplacement); + zreplacement = + PyBytes_FromStringAndSize(buf, + strlen(buf)); + if (zreplacement == NULL) + goto Done; + } + } + assert(zreplacement != NULL); + ptoappend = PyBytes_AS_STRING(zreplacement); + ntoappend = PyBytes_GET_SIZE(zreplacement); + } else if (ch == 'z') { if (zreplacement == NULL) { /* format utcoffset */ @@ -1214,7 +1258,7 @@ sizeof(buf), "", tzinfo, - tzinfoarg) < 0) + tzinfoarg, Py_False) < 0) goto Done; Py_DECREF(zreplacement); zreplacement = @@ -3635,7 +3679,7 @@ /* We need to append the UTC offset. */ if (format_utcoffset(buf, sizeof(buf), ":", self->tzinfo, - Py_None) < 0) { + Py_None, Py_False) < 0) { Py_DECREF(result); return NULL; } @@ -4512,7 +4556,7 @@ /* We need to append the UTC offset. */ if (format_utcoffset(buffer, sizeof(buffer), ":", self->tzinfo, - (PyObject *)self) < 0) { + (PyObject *)self, Py_False) < 0) { Py_DECREF(result); return NULL; }