Skip to content

Commit 1c23f5a

Browse files
committed
gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F
1 parent 588be7a commit 1c23f5a

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lib/_pydatetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ def _wrap_strftime(object, format, timetuple):
273273
# Note that datetime(1000, 1, 1).strftime('%G') == '1000' so
274274
# year 1000 for %G can go on the fast path.
275275
elif ((ch in 'YG' or ch in 'FC') and
276-
object.year < 1000 and _need_normalize_century()):
276+
timetuple[0] < 1000 and _need_normalize_century()):
277277
if ch == 'G':
278278
year = int(_time.strftime("%G", timetuple))
279279
else:
280-
year = object.year
280+
year = timetuple[0]
281281
if ch == 'C':
282282
push('{:02}'.format(year // 100))
283283
else:

Lib/test/datetimetester.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,6 +4084,13 @@ def test_strftime(self):
40844084
# A naive object replaces %z, %:z and %Z with empty strings.
40854085
self.assertEqual(t.strftime("'%z' '%:z' '%Z'"), "'' '' ''")
40864086

4087+
# gh-152305: the year directives must not raise on a time (1900-01-01).
4088+
t1230 = self.theclass(12, 30)
4089+
self.assertEqual(t1230.strftime('%Y'), '1900')
4090+
self.assertEqual(t1230.strftime('%G'), '1900')
4091+
self.assertEqual(t1230.strftime('%C'), '19')
4092+
self.assertEqual(t1230.strftime('%F'), '1900-01-01')
4093+
40874094
# bpo-34482: Check that surrogates don't cause a crash.
40884095
try:
40894096
t.strftime('%H\ud800%M')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`datetime.time.strftime` raising :exc:`AttributeError` for the
2+
``%Y``, ``%G``, ``%C`` and ``%F`` directives in the pure-Python
3+
implementation. Patch by tonghuaroot.

0 commit comments

Comments
 (0)