[Tutor] Working with dates

Kent Johnson kent_johnson at skillsoft.com
Sun Oct 17 14:44:35 CEST 2004


At 10:06 PM 10/17/2004 +1300, Liam Clarke wrote:
>Thanks all for your responses,
>
>I think one of my weaknesses in this is a poor understanding of how to
>properly utilise classes.

Or more specifically a lack of understanding of the classes in the standard 
library :-)

There are many, many very useful classes included with Python. Most of them 
are documented in the Library Reference at 
http://docs.python.org/lib/lib.html. Browsing this reference is a good way 
to learnabout what Python can do.

>'lastFriday=datetime.date.today()' I gather this creates a date
>instance called lastFriday and gives it today's date/time values?
>
>'while lastFriday.weekday() != 4:'  and I'm guessing that the method
>weekday in class date generates an integer value for day of the week,
>and that it generates that based on the stored date in lastFriday.

Take a look at the docs for the datetime module. The easiest way to look up 
a specific module is from the Global Module Index 
(http://docs.python.org/modindex.html). This takes us to the datetime 
module and the page for datetime.date. There you will find the entries
today( )
  Return the current local date. This is equivalent to 
date.fromtimestamp(time.time()).

and
weekday( )
  Return the day of the week as an integer, where Monday is 0 and Sunday is 
6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday.

So your guesses are correct.

>'lastFriday -= oneday' I don't quite understand what  -= does.
>It's not a boolean/arithmetic operator? Is it peculiar to the datetime
>module? I'm guessing it increments the date stored in lastFriday back
>by period oneday?
>
>what is different between - and -= ?

In general for any binary operator <op>,
x <op>= y
is the same as
x = x <op> y

So
lastFriday -= oneday
is the same as
lastFriday = lastFriday - oneday

>'print lastFriday.strftime('%d-%b-%Y')' and of course, you can
>generate a string direct from lastFriday using the strftime method...
>( because datetime supercedes the date and time modules?)

It doesn't supercede them, but it does duplicate some functionality. I 
think the intent when datetime was added was to make it easier to work with 
dates and times.

>It's all very clear to me now, hindsight is great that way.
>
>(Just spent ten minutes rereading manual on date class, and it now
>makes more sense, this OOP thing.)
>
>I read the tutorials on OOP, but it kind of went over my head.
>
>So an instance of the class date, can hold variables such as year etc.
>and then run a contained function (like strftime) on that variable....

Exactly

>the light dawns....
>
>Danny, I don't quite understand what datetime.date.resolution does
>exactly. Your function generates the targetTime, and checks that it
>hasn't already happened, but if it does it adds DAYDELTA?

datetime.date.resolution is the same as my oneday variable, it is the time 
interval of a day. Danny's code makes a date object that represents 8am 
today; if that time is in the past then it changes it to 8am tomorrow by 
adding datetime.date.resolution.

Kent


>Thank you for your knowledge and help. I'm learning heaps.
>And Kent, your code does exactly what I need, and so elegantly too.
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list