strftime - %a is always Monday ?

Discussion in 'Python' started by Richard Shea, Dec 29, 2003.

  1. Richard Shea

    Richard Shea Guest

    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.
     
    Richard Shea, Dec 29, 2003
    #1
    1. Advertising

  2. Richard Shea

    Gerrit Holl Guest

    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/
     
    Gerrit Holl, Dec 29, 2003
    #2
    1. Advertising

  3. Richard Shea

    Jeff Epler Guest

    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
     
    Jeff Epler, Dec 29, 2003
    #3
  4. Richard Shea

    Jeff Epler Guest

    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
     
    Jeff Epler, Dec 29, 2003
    #4
  5. Richard Shea

    Peter Otten Guest

    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
     
    Peter Otten, Dec 29, 2003
    #5
  6. Richard Shea

    Richard Shea Guest

    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.
     
    Richard Shea, Dec 30, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Allen

    Thought For a Monday

    Scott Allen, Apr 4, 2005, in forum: ASP .Net
    Replies:
    12
    Views:
    558
    Kevin Spencer
    Apr 5, 2005
  2. Jeremy Rayner
    Replies:
    0
    Views:
    391
    Jeremy Rayner
    Apr 14, 2004
  3. sunspot
    Replies:
    4
    Views:
    12,925
    sunspot
    May 19, 2005
  4. melledge
    Replies:
    0
    Views:
    371
    melledge
    Jan 3, 2006
  5. best
    Replies:
    0
    Views:
    212
Loading...

Share This Page