[3.13] Revert "[3.13] gh-120713: Normalize year with century for datetime.strftime (GH-120820) (GH-121144)" by serhiy-storchaka · Pull Request #122408 · python/cpython
#ifdef Py_NORMALIZE_CENTURY /* Buffer of maximum size of formatted year permitted by long. */ char buf[SIZEOF_LONG*5/2+2]; #endif
assert(object && format && timetuple); assert(PyUnicode_Check(format)); /* Convert the input format to a C string and size */ pin = PyUnicode_AsUTF8AndSize(format, &flen); if (!pin) return NULL;
PyObject *strftime = _PyImport_GetModuleAttrString("time", "strftime"); if (strftime == NULL) { goto Done; }
/* Scan the input format, looking for %z/%Z/%f escapes, building * a new format. Since computing the replacements for those codes * is expensive, don't unless they're actually used.
if (year_long == -1 && PyErr_Occurred()) { goto Done; } /* Note that datetime(1000, 1, 1).strftime('%G') == '1000' so year 1000 for %G can go on the fast path. */ if (year_long >= 1000) { goto PassThrough; } if (ch == 'G') { PyObject *year_str = PyObject_CallFunction(strftime, "sO", "%G", timetuple); if (year_str == NULL) { goto Done; } PyObject *year = PyNumber_Long(year_str); Py_DECREF(year_str); if (year == NULL) { goto Done; } year_long = PyLong_AsLong(year); Py_DECREF(year); if (year_long == -1 && PyErr_Occurred()) { goto Done; } }
ntoappend = PyOS_snprintf(buf, sizeof(buf), "%04ld", year_long); ptoappend = buf; } #endif else { /* percent followed by something else */ #ifdef Py_NORMALIZE_CENTURY PassThrough: #endif ptoappend = pin - 2; ntoappend = 2; }
if (strftime == NULL) goto Done; format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt)); if (format != NULL) { result = PyObject_CallFunctionObjArgs(strftime, format, timetuple, NULL); Py_DECREF(format); } Py_DECREF(strftime); } Done: Py_XDECREF(freplacement); Py_XDECREF(zreplacement); Py_XDECREF(colonzreplacement); Py_XDECREF(Zreplacement); Py_XDECREF(newfmt); Py_XDECREF(strftime); return result; }