strftime - %a is always Monday ?

R

Richard Shea

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 ?


thanks

richard shea.
 
G

Gerrit Holl

Richard said:
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 ?

time.strftime does not know about dates. Field number 6 specifies the
weekday: make it a 1 and it will say Tuesday, 2->Wednesday, etc. You may
want to use the new datetime module:'Sunday'

yours,
Gerrit.
 
J

Jeff Epler

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
 
J

Jeff Epler

time.strftime does not know about dates. Field number 6 specifies the
weekday: make it a 1 and it will say Tuesday, 2->Wednesday, etc. You may
want to use the new datetime module:
'Sunday'

Gerrit,
Sounds like much better advice than mine!

Jeff
 
P

Peter Otten

Richard said:
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:
.... 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:

Peter
 
R

Richard Shea

Thanks very much to all of you, the code is working nicely now and I
prefer the "cleaner look" of ...

datetime.date(2003, 12, 27).strftime("%a")

.... as opposed to what I was attempting.

thanks again.

richard shea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top