strftime - %a is always Monday ?

Jeff Epler jepler at unpythonic.net
Mon Dec 29 08:44:50 EST 2003


strftime obeyes the exact values specified in its arguments.  You
specify that tm_wday is 0, so it prints monday.

If you want to start with a partial time specification (at least
year/month/day), you need to first use mktime() to convert it to
seconds-since-epoch, then back to the tuple representation with
localtime().

>>> from time import *
>>> lst1 = ['2003', '12', '27']
>>> tm = (int(lst1[0]), int(lst1[1]), int(lst1[2]), 0, 0, 0, 0, 0, 0)
>>> print strftime("%A, %d (%w %y %m)", tm)
Monday, 27 (1 03 12)
>>> t = mktime(tm)
>>> tm1 = localtime(t)
>>> print tm1
(2003, 12, 27, 0, 0, 0, 5, 361, 0)
>>> print strftime("%A, %d (%w %y %m)", tm1)
Saturday, 27 (6 03 12)

Jeff





More information about the Python-list mailing list