[issue8026] strftime bug when timedelta is negative

Alexander Belopolsky report at bugs.python.org
Mon Apr 12 16:35:34 CEST 2010


Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:

This is not a bug.  The issue boils down to the following:

>>> from datetime import *
>>> d = datetime(2010, 1, 1)
>>> d, d.strftime("%G")
(datetime.datetime(2010, 1, 1, 0, 0), '2009')

and OP expects '2010' instead.  Python behavior is correct as can be verified using GNU date utility:

$ date --date=20100101 +%G
2009

The confusion comes from the fact that %G is formatted as the ISO 8601 year.  This year is the one that contains the greater part of the week (Monday as the first day of the week).


Here is another illustration:
>>> d = date(2009,12,31)
>>> for i in range(7):
...    (d+timedelta(i)).strftime("%F %a %G %V")
... 
'2009-12-31 Thu 2009 53'
'2010-01-01 Fri 2009 53'
'2010-01-02 Sat 2009 53'
'2010-01-03 Sun 2009 53'
'2010-01-04 Mon 2010 01'
'2010-01-05 Tue 2010 01'
'2010-01-06 Wed 2010 01'

Note that week 1 of 2010 started on Monday, 2010-01-04.

OP is advised to change %G to %Y in his code.

----------
nosy: +belopolsky

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8026>
_______________________________________


More information about the Python-bugs-list mailing list