473,503 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strftime - %a is always Monday ?

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.
Jul 18 '05 #1
5 1625
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
Jul 18 '05 #2
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 ?


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:
datetime.date(2003,12,7).strftime("%A")

'Sunday'

yours,
Gerrit.

--
131. If a man bring a charge against one's wife, but she is not
surprised with another man, she must take an oath and then may return to
her house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #3
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

Jul 18 '05 #4
On Mon, Dec 29, 2003 at 02:39:13PM +0100, Gerrit Holl wrote:
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:
datetime.date(2003,12,7).strftime("%A")

'Sunday'


Gerrit,
Sounds like much better advice than mine!

Jeff

Jul 18 '05 #5
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.
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2242
by: Allen Unueco | last post by:
I feel that the '%Z' format specifier from strftime() returns the wrong value when daylight savings is in effect. Today the following is always true: time.strftime('%Z') == time.tzname ...
4
9267
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
5
2921
by: Sheila King | last post by:
I have a web app that has been running just fine for several months under Python 2.2.2. We are preparing to upgrade the server to run Python 2.4.1. However, part of my web app is throwing an...
4
2875
by: Andy Leszczynski | last post by:
Python 2.2/Unix >>time.strftime("%T") '22:12:15' >>time.strftime("%X") '22:12:17' Python 2.3/Windows >>time.strftime("%X")
2
3943
by: Mike Conmackie | last post by:
Greetings, Is there any way to force strftime() to ignore locale settings when formatting the resulting string? I have a requirement to create a specific date-time string format in UTC. ...
2
3035
by: Darko | last post by:
Hi, by definition, strftime() uses the previous setlocale() calls to determine which language to use in returning strings such as week days or month names, depending on the given format. Now,...
12
4454
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there...
3
7645
by: Andrii V. Mishkovskyi | last post by:
2008/5/7 Alexandr N Zamaraev <tonal@promsoft.ru>: Unicode and str objects are not the same. Why do you think that this is a bug? Anyway, you can always use 'encode' method of unicode objects: ...
6
7669
by: kaydubbleu | last post by:
I have stumbled across what I think seems to be a weird situation, and was hoping that maybe someone out there could point me in the direction as to why it is occuring. $date = '2008-09-16'; ...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.