Brian van den Broek wrote at 16:53 12/5/2004:
Dick Moores said unto the world upon 2004-12-05 15:03:
Thanks, Brian. I looked at your code a long time, and also read the 11/26 thread you started. I can see how I could use datetime() and your t2 - t1 to get the seconds for time.sleep(), but the resulting code I have in mind is more convoluted than the heart of my timer3.py, which I quote below. (I don't need the alarm time to be more than 24 hours from current time--therefore I want to ignore the year, month, and day.)
=======================================
import time
alarm = raw_input("Enter alarm time as hhmm: ")
now = time.strftime("%X") # produces current time in format hh:mm:ss
nowSecond = int(now[6:])
nowMinute = int(now[3:5])
nowHour = int(now[0:2])
alarmMinute = int(alarm[2:4])
alarmHour = int(alarm[0:2])
hoursDiff = alarmHour - nowHour
minutesDiff = alarmMinute - nowMinute
if hoursDiff < 0 or (hoursDiff == 0 and minutesDiff <= 0):
hoursDiff = hoursDiff + 24 # add a day
sleepSeconds = hoursDiff*3600 + minutesDiff*60 - nowSecond
time.sleep(sleepSeconds)
====================================
If I'm wrong, could someone please set me right?
Dick
Hi Dick and all,
sorry I was too lazy to follow your link before, Dick. Thanks for posting the relevant portions.
I took another run, but my code is a lot longer as I put in some error checking on the input request -- hope you don't mind ;-) (I might have gone overboard -- I did it to learn how as much as anything else.)
I suspect that my way is easier than yours. (I don't know about Liam's. His came in as I was writing mine, and I've not read his closely yet.)
<SNIP Including code with comments and some (incomplete) error checking>
Brian,
So yours can be boiled down to ==========Begin code================== 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==================
Yes, I think yours is shorter, but not simpler. Mine doesn't need to consider the year or month, or leap years. On the other hand, mine doesn't take care of crossing the daylight time change borderline.
I can see your point; you're right that it made me think about leap years, etc. in a way that your code avoids.
But thanks very much. It gives me some understanding of the datetime module. As does Liam's code.
BTW I found one omission in your error checking. The case where the user enters the time without a colon, e.g., 1234 instead of 12:34.
Dick
Thanks! You're right. I was going to move it over to one of my utility modules, so I'm glad to know about the bug.
Best to all,
Brian vdB
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor