strftime - %a is always Monday ?

Peter Otten __peter__ at web.de
Mon Dec 29 08:54:25 EST 2003


Richard Shea wrote:

> Hi - I'm trying to use strftime to output the day of the week but I
> find that I always get told it's Monday. I have tried day, month, year
> etc and all come out correctly but as soon as I use %a I get 'Mon'.
> I'm running Python 2.3.2 on a Windows 98 machine. Can anyone suggest
> what the problem might be please ?
> 
> This is a segment of the code which is manfests the behaviour ...
> 
>>>> from time import localtime,strftime,time
>>>> lst1 = ['2003','12','27']
>>>> strftime("%A,%d (%w %y
> %m)",[int(lst1[0]),int(lst1[1]),int(lst1[2]),0,0,0,0,0,0])
> 'Monday,27 (1 03 12)'
>>>> 
> 
> ... whereas the 27th was a Saturday ?

Garbage in garbage out. Only the 7th element of the tuple is used to
generate the string:

>>> for wd in range(7):
...     print time.strftime("%a", (0,0,0,0,0,0,wd,0,0))
...
Mon
Tue
Wed
Thu
Fri
Sat
Sun

The cleanest solution uses the new datetime module instead:

>>> import datetime
>>> datetime.date(2003, 12, 27).strftime("%a")
'Sat'
>>>

Peter




More information about the Python-list mailing list