[Tutor] finding difference in time

R K wolf85boy2008 at yahoo.com
Fri May 15 06:46:49 CEST 2009


Gurus,

I'm trying to write a fairly simple script that finds the number of hours / minutes / seconds between now and the next Friday at 1:30AM.

I have a few little chunks of code but I can't seem to get everything to piece together nicely.

import datetime,time
now = datetime.datetime.now()

i = 0
dayOfWeek = datetime.datetime.now().strftime( '%a' )
while dayOfWeek != 'Fri':
    delta = datetime.timedelta( days = i )
    tom = ( now + delta ).strftime( '%a' )
    if tom != 'Fri':
        i = i + 1
    else:
        print i
        print tom
        break

So with this code I can determine the number of days until the next Friday (if it's not Friday already).

The problem I'm having, however, is with finding the number of minutes until 1:30AM on Friday.

nextFridayDay = int( now.strftime( '%d' ) ) + 1
nextFridayMonth = int( now.strftime( '%m' ) )
nextFridayYear = int( now.strftime( '%Y' ) )

nextRun = datetime.datetime( nextFridayYear , nextFridayMonth , nextFridayDay , 1 , 30 , 0 )

What I gather is that I should be able to numerically manipulate two datetime objects, as seen below:

In [227]: nextRun - now  
Out[227]: datetime.timedelta(0, 46155, 51589)

The result, however, doesn't make sense. Take a look...

In [231]: d = nextRun - now

In [232]: d.seconds
Out[232]: 46155

In [233]: d.days
Out[233]: 0

Thoughts on what I may be doing wrong? Am I going about this the whole wrong way? Should I be using something different to calculate the number of minutes between now and the next Friday at 1:30AM?



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090514/74e221a8/attachment.htm>


More information about the Tutor mailing list