[Tutor] finding difference in time
Dave Angel
davea at ieee.org
Fri May 15 13:20:42 CEST 2009
R K <wolf85boy2008 at yahoo.com> wrote:
> 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?
>
Could you give us the exact wording of the homework assignment? I
suspect that if now is on a Friday, and already past 1:30, then you
want to target a week from today, rather than getting negative numbers.
If so, you can't treat day-of-week as something independent. If you
want to keep your present loop, you should first compare now's hour and
minute to 1:30.
Second problem, you're not using the result of the loop. You could use
either delta or i as some indication of how many days are between now
and Friday. I suspect you intended for the +1 on nextFridayDay to be +i
instead. Unfortunately, that would help any time except during the last
week of the month (and year).
Fixing each of these in isolation would make the code painfully convoluted.
Seems to me you should be finding your target date as a timedate object,
and manipulating that object directly till it represents a time of 1:30,
a day of Friday, and a delta that's positive.
So as an outline:
make a copy of now, and call it target.
change target's time fields to be exactly 1:30
compare target to now, and if smaller, increment it by a delta of
exactly one day
in a loop, check if target's day-of-week is 'Fri' and increment by
another day if not
Now you have final target, so do your difference.
More information about the Tutor
mailing list