[Tutor] Working with dates

Kent Johnson kent_johnson at skillsoft.com
Sun Oct 17 06:44:21 CEST 2004


That's pretty complicated. What about this?

import datetime

lastFriday = datetime.date.today()
oneday = datetime.timedelta(days=1)

while lastFriday.weekday() != 4:
     lastFriday -= oneday

print lastFriday.strftime('%d-%b-%Y')

Kent

At 03:11 PM 10/17/2004 +1300, Liam Clarke wrote:
>Kia ora all,
>
>I am trying to write a function that will get today's date when run,
>and set a string to the date of a day in the previous week.
>
>i.e. it's Saturday the 16th of October, and I want to find the date of
>Friday last week.
>I want to generate a date string that is valid for the IMAP4 protocol,
>so it must be in format
>16-Oct-2004.
>
>It will be run once a week, usually on a Monday, usually looking for
>the last Friday. I want it to be able to handle Friday being in a
>different month or year and still give a precise date.
>
>This is what I've done so far, but I'm sure there's a more efficient
>way to do it, so feedback is gratefully received.
>
>A little note on day_offset -
>
>gmtime() returns a integer representing the day of the week, Monday to
>Sunday is 0 to 6.
>
>So, I set up a wee index spanning two weeks like so: -
>
>  M  T  W  T   F  S  S   M  T  W  T  F  S  S
>-7  -6  -5  -4 -3  -2  -1 ( 0   1   2  3  4   5  6)
>
>The numbers in brackets are returned by calendar.weekday, the negative
>ones are my constructs.
>
>import time
>import datetime
>
>day_offset=-3 #Friday of previous week, will be changable by user in config
>
>gm_data=time.gmtime() #Get today's date/time
>
>(year, month, day, weekday)=gm_data[0], gm_data[1], gm_data[2],
>gm_data[6]  #Only need first 3 values and 7th, which is weekday.
>
>date_offset = -(day_offset-weekday)
>
># This is to work with datetime.timedelta,which works with positive days.
># so it works out to date_offset = - ( -3 - 5) = 8
>
>last_friday=datetime.date(year, month, day) -
>datetime.timedelta(days=date_offset)
>
>#Create date object for today's date, to use Python's built handling
>of date arithmetic.
>#timedelta is difference, so last_friday = today's date - 8 days
>
>retrieve_date_string=str(last_friday)
>#last_friday is an object, I need a string so that I can reformat the date
>(last_friday outputs as 2004-10-08)
>
>split_date=retrieve_date_string.split('-')
>
>email_sent_date=time.strftime("%d-%b-%Y", (int(split_date[0]),
>int(split_date[1]), int(split_date[2]), 0, 0, 0, 0, 0, 0))
>
>(time.strftime looks for a tuple of (year, month, day, hour, minute,
>second, weekday, day of year, and something called tm_isdst)  I can't
>pass it my last_friday object, so I have to generate a tuple for last
>Friday. gmtime() generates the 9 value tuple normally.)
>
>And email_sent_date = "8-Oct-2004"
>
>Am I using redundant code anywhere? I just found a redundancy in that
>I was using gmtime() to get my year month and day, but was using
>calendar.weekday to get my week day value, when gmtime() generates it
>anyway.
>
>I can see a problem here, if this is run on the Saturday or Sunday, it
>won't get the Friday just been, but the Friday previously. So, I may
>just add a catcher for if weekday >= 5
>
>Thank-you for your time
>
>Liam Clarke
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list