@@ -1740,6 +1740,42 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
17401740 return 0 ;
17411741}
17421742
1743+ /* Check whether year with century should be normalized for strftime. */
1744+ inline static int
1745+ normalize_century (void )
1746+ {
1747+ static int _normalize_century = -1 ;
1748+ if (_normalize_century < 0 ) {
1749+ char year [5 ];
1750+ struct tm date = {
1751+ .tm_year = -1801 ,
1752+ .tm_mon = 0 ,
1753+ .tm_mday = 1
1754+ };
1755+ _normalize_century = (strftime (year , sizeof (year ), "%Y" , & date ) &&
1756+ strcmp (year , "0099" ) != 0 );
1757+ }
1758+ return _normalize_century ;
1759+ }
1760+
1761+ /* Check whether C99-specific strftime specifiers are supported. */
1762+ inline static int
1763+ strftime_c99_support (void )
1764+ {
1765+ static int _strftime_c99_support = -1 ;
1766+ if (_strftime_c99_support < 0 ) {
1767+ char full_date [11 ];
1768+ struct tm date = {
1769+ .tm_year = 0 ,
1770+ .tm_mon = 0 ,
1771+ .tm_mday = 1
1772+ };
1773+ _strftime_c99_support = (strftime (full_date , sizeof (full_date ), "%F" , & date ) &&
1774+ strcmp (full_date , "1900-01-01" ) == 0 );
1775+ }
1776+ return _strftime_c99_support ;
1777+ }
1778+
17431779static PyObject *
17441780make_somezreplacement (PyObject * object , char * sep , PyObject * tzinfoarg )
17451781{
@@ -1910,12 +1946,9 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
19101946 }
19111947 replacement = freplacement ;
19121948 }
1913- #ifdef Py_NORMALIZE_CENTURY
1914- else if (ch == 'Y' || ch == 'G'
1915- #ifdef Py_STRFTIME_C99_SUPPORT
1916- || ch == 'F' || ch == 'C'
1917- #endif
1918- ) {
1949+ else if (normalize_century () && (ch == 'Y' || ch == 'G' ||
1950+ (strftime_c99_support () && (ch == 'F' || ch == 'C' ))))
1951+ {
19191952 /* 0-pad year with century as necessary */
19201953 PyObject * item = PySequence_GetItem (timetuple , 0 );
19211954 if (item == NULL ) {
@@ -1952,15 +1985,11 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
19521985 * +6 to accommodate dashes, 2-digit month and day for %F. */
19531986 char buf [SIZEOF_LONG * 5 / 2 + 2 + 6 ];
19541987 Py_ssize_t n = PyOS_snprintf (buf , sizeof (buf ),
1955- #ifdef Py_STRFTIME_C99_SUPPORT
19561988 ch == 'F' ? "%04ld-%%m-%%d" :
1957- #endif
19581989 "%04ld" , year_long );
1959- #ifdef Py_STRFTIME_C99_SUPPORT
19601990 if (ch == 'C' ) {
19611991 n -= 2 ;
19621992 }
1963- #endif
19641993 if (_PyUnicodeWriter_WriteSubstring (& writer , format , start , end ) < 0 ) {
19651994 goto Error ;
19661995 }
@@ -1970,7 +1999,6 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
19701999 }
19712000 continue ;
19722001 }
1973- #endif
19742002 else {
19752003 /* percent followed by something else */
19762004 continue ;
0 commit comments