Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8bb466

Browse files
committed
Issue #1777412: extended year range of strftime down to 1000.
1 parent 9253214 commit b8bb466

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

Lib/datetime.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def _format_time(hh, mm, ss, us):
173173
# Correctly substitute for %z and %Z escapes in strftime formats.
174174
def _wrap_strftime(object, format, timetuple):
175175
year = timetuple[0]
176-
if year < 1900:
177-
raise ValueError("year=%d is before 1900; the datetime strftime() "
178-
"methods require year >= 1900" % year)
176+
if year < 1000:
177+
raise ValueError("year=%d is before 1000; the datetime strftime() "
178+
"methods require year >= 1000" % year)
179179
# Don't call utcoffset() or tzname() unless actually needed.
180180
freplace = None # the string to use for %f
181181
zreplace = None # the string to use for %z
@@ -1189,7 +1189,7 @@ def strftime(self, fmt):
11891189
"""Format using strftime(). The date part of the timestamp passed
11901190
to underlying strftime should not be used.
11911191
"""
1192-
# The year must be >= 1900 else Python's strftime implementation
1192+
# The year must be >= 1000 else Python's strftime implementation
11931193
# can raise a bogus exception.
11941194
timetuple = (1900, 1, 1,
11951195
self._hour, self._minute, self._second,

Lib/test/datetimetester.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,10 @@ def test_bool(self):
12841284
self.assertTrue(self.theclass.max)
12851285

12861286
def test_strftime_out_of_range(self):
1287-
# For nasty technical reasons, we can't handle years before 1900.
1287+
# For nasty technical reasons, we can't handle years before 1000.
12881288
cls = self.theclass
1289-
self.assertEqual(cls(1900, 1, 1).strftime("%Y"), "1900")
1290-
for y in 1, 49, 51, 99, 100, 1000, 1899:
1289+
self.assertEqual(cls(1000, 1, 1).strftime("%Y"), "1000")
1290+
for y in 1, 49, 51, 99, 100, 999:
12911291
self.assertRaises(ValueError, cls(y, 1, 1).strftime, "%Y")
12921292

12931293
def test_replace(self):

Modules/_datetimemodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
11661166
if (!pin)
11671167
return NULL;
11681168

1169-
/* Give up if the year is before 1900.
1169+
/* Give up if the year is before 1000.
11701170
* Python strftime() plays games with the year, and different
11711171
* games depending on whether envar PYTHON2K is set. This makes
1172-
* years before 1900 a nightmare, even if the platform strftime
1172+
* years before 1000 a nightmare, even if the platform strftime
11731173
* supports them (and not all do).
11741174
* We could get a lot farther here by avoiding Python's strftime
11751175
* wrapper and calling the C strftime() directly, but that isn't
@@ -1182,10 +1182,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
11821182
assert(PyLong_Check(pyyear));
11831183
year = PyLong_AsLong(pyyear);
11841184
Py_DECREF(pyyear);
1185-
if (year < 1900) {
1185+
if (year < 1000) {
11861186
PyErr_Format(PyExc_ValueError, "year=%ld is before "
1187-
"1900; the datetime strftime() "
1188-
"methods require year >= 1900",
1187+
"1000; the datetime strftime() "
1188+
"methods require year >= 1000",
11891189
year);
11901190
return NULL;
11911191
}
@@ -3663,7 +3663,7 @@ time_strftime(PyDateTime_Time *self, PyObject *args, PyObject *kw)
36633663

36643664
/* Python's strftime does insane things with the year part of the
36653665
* timetuple. The year is forced to (the otherwise nonsensical)
3666-
* 1900 to worm around that.
3666+
* 1900 to work around that.
36673667
*/
36683668
tuple = Py_BuildValue("iiiiiiiii",
36693669
1900, 1, 1, /* year, month, day */

Modules/timemodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ time_strftime(PyObject *self, PyObject *args)
471471
return NULL;
472472

473473
/* XXX: Reportedly, some systems have issues formating dates prior to year
474-
* 1900. These systems should be identified and this check should be
474+
* 1000. These systems should be identified and this check should be
475475
* moved to appropriate system specific section below. */
476-
if (buf.tm_year < 0) {
476+
if (buf.tm_year < -900) {
477477
PyErr_Format(PyExc_ValueError, "year=%d is before 1900; "
478478
"the strftime() method requires year >= 1900",
479479
buf.tm_year + 1900);

0 commit comments

Comments
 (0)