[Tutor] Re: Could I have used time or datetime modules here?

Dick Moores rdm at rcblue.com
Tue Dec 7 13:04:33 CET 2004


To Liam and Brian,

Liam, actually, Brian did Sunday. See his post in this thread of Date: 
Sun, 05 Dec 2004 19:53:01 -0500. But you've done is interesting, and I 
thank you.

Here's Brian's script in it's bare bones, without the input error 
checking and his extensive and helpful comments:

===============begin code====================
import datetime
import time

alarm_time = raw_input("Enter alarm time as hh:mm ")
alarm_time_list = alarm_time.split(':')
alarm_hour, alarm_minute = (int(alarm_time_list[0]),
                             int(alarm_time_list[1])) 

now = datetime.datetime.now()
alarm_datetime = datetime.datetime(now.year+4, now.month, now.day,
                                    alarm_hour, alarm_minute)
print alarm_datetime
alarm_in_seconds = (alarm_datetime - now).seconds
print "I should wake up in %d seconds" % alarm_in_seconds
time.sleep(alarm_in_seconds)
print "I'm awake!"
================end code=====================

You can see that he gets the number-of-seconds difference directly with 
the line,

"alarm_in_seconds = (alarm_datetime - now).seconds",

and doesn't need to compute the seconds to sleep from the difference 
expressed as hours, minutes and seconds.

I like your alarm clock a lot, 
<http://www.rafb.net/paste/results/ctOH1T36.html>. I first tried to run 
it with IDLE, then remembered that mscvrt won't catch keypresses if run 
with IDLE. Works fine in the console!

Instead of your beeps in the second while loop, I think I'd need

winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
winsound.PlaySound('SystemHand', winsound.SND_ALIAS)

and turn my speakers way up!


Brian, where did you learn about the ".seconds". And the .year, .month, 
.day of

"alarm_datetime = datetime.datetime(now.year + 4, now.month, now.day,
                                    alarm_hour, alarm_minute)"?

Does this come from a general knowledge of OOP, or is it somewhere in the 
Python docs? The only thing I've seen, and it's not an explanation, is in 
note (1) on http://docs.python.org/lib/datetime-date.html

It seems I've missed out on something important

BTW I'm not sure you need the +4 of "now.year + 4". I've run this without 
the +4 and it doesn't seem to be needed. And notes (1) and (4) on that 
page seem to say this, as far as I understand them.

Dick


Liam Clarke wrote at 03:56 12/6/2004:
>Hey Dick, don't know if anyone actually answered your original question.
>
> >IOW, is there an easier way to calculate the time difference between the
> >time now, say 08:51 and say, tomorrow at 03:45, to take an example of the
> >most difficult case?
>
>So, you need two datetime.datetime objects.
>
> >>>now = datetime.datetime(year, month, day, hours, minutes, seconds)
> >>>now = datetime.datetime(2004, 12, 7*, 8*, 51, 00)
> >>> later= datetime.datetime(2004, 12, 8*, 3*, 45, 00)
>
>  *Can't start with zero. Must be 8 not 08
>
> >>> difference = later - now
> >>> print difference
>18:54:00
> >>> type(difference)
><type 'datetime.timedelta'>
> >>> timeList=str(difference).split(":")
> >>> print timeList
>['18', '54', '00']
> >>> 
> timeinSecs=(int(timeList[0])*3600)+(int(timeList[1])*60)+int(timeList[2])
> >>> print timeinSecs
>68040
>
>Now, to check if that's right...
>
> >>> timeChange=datetime.timedelta(seconds=68040)
> >>> checkVal=now + timeChange
> >>> print checkVal
>2004-12-08 03:45:00
>
>Looks good to me.
>
>So, to summarise the code part -
>
>now = datetime.datetime(2004, 12, 7, 8, 51, 00)
>later= datetime.datetime(2004, 12, 8, 3, 45, 00)
>difference = later - now
>timeList=str(difference).split(":")
>timeinSecs=(int(timeList[0])*3600)+(int(timeList[1])*60)+int(timeList[2])
>
>And that's the easier way to find the difference between two times in 
>seconds.
>
>HTH
>
>Liam Clarke
>
>P.S. If you're interested, here's a 20 line alarm clock I wrote
>because my one broke this morning.
>
>http://www.rafb.net/paste/results/ctOH1T36.html




More information about the Tutor mailing list