[Tutor] finding difference in time
Andre Engels
andreengels at gmail.com
Fri May 15 09:28:00 CEST 2009
On Fri, May 15, 2009 at 6:46 AM, 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?
Why do you think the result doesn't make sense? You basically defined
"next Friday" as tomorrow (the day in the same year on the same day
one number more), and then looked at the time until next Friday at
1:30AM. It appears that that time was 0 days and 46155 seconds (plus
some fraction of a second, which is about 12 hours and 50 minutes.
--
André Engels, andreengels at gmail.com
More information about the Tutor
mailing list