[Tutor] Re: Could I have used time or datetime modules
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
Here's Brian's script in it's bare bones, without the input error
checking and his extensive and helpful comments:
alarm_time = raw_input("Enter alarm time as hh:mm ")
alarm_time_list = alarm_time.split(':')
alarm_hour, alarm_minute = (int(alarm_time_list),
now = datetime.datetime.now()
alarm_datetime = datetime.datetime(now.year+4, now.month, now.day,
alarm_in_seconds = (alarm_datetime - now).seconds
print "I should wake up in %d seconds" % alarm_in_seconds
print "I'm awake!"
You can see that he gets the number-of-seconds difference directly with
"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
and turn my speakers way up!
Brian, where did you learn about the ".seconds". And the .year, .month,
"alarm_datetime = datetime.datetime(now.year + 4, now.month, now.day,
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.
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
> >>> type(difference)
> >>> timeList=str(difference).split(":")
> >>> print timeList
>['18', '54', '00']
> >>> print timeinSecs
>Now, to check if that's right...
> >>> timeChange=datetime.timedelta(seconds=68040)
> >>> checkVal=now + timeChange
> >>> print checkVal
>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
>And that's the easier way to find the difference between two times in
>P.S. If you're interested, here's a 20 line alarm clock I wrote
>because my one broke this morning.
More information about the Tutor