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

Liam Clarke cyresse at gmail.com
Tue Dec 7 18:52:21 CET 2004


False, I meant false. Ack, need coffee.

>while nowTime < desiredTime:

>because, I wrote the alarm clock after midnight! If I had run it
>before 11:59pm my while condition would've been True immediately, so I
>changed desiredTime to a datetime object.


On Wed, 8 Dec 2004 06:50:22 +1300, Liam Clarke <cyresse at gmail.com> wrote:
> Hi all,
> 
> 
> 
> >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!
> 
> 
> Heh, I use headphones,  but speakers would be better. I'm sure you
> could play an mp3 also.
> 
> But, I actually made a mistake in my alarm clock in that
> 
> desiredTime.datetime.time(6,00,00)
> is now desiredTime=datetime.datetime(2004,12,8,6, 00, 00)
> 
> while nowTime.time < desiredTime:
> 
> is now
> 
> while nowTime < desiredTime:
> 
> because, I wrote the alarm clock after midnight! If I had run it
> before 11:59pm my while condition would've been True immediately, so I
> changed desiredTime to a datetime object.
> 
> Alternatively, I could have just changed
> 
> checkPeriod=5 #minutes
> nowTime=datetime.datetime.now()
> startTime=nowTime
> 
> while nowTime.time() < desiredTime or nowTime.time() > startTime.time():
> 
> But Occam's Razor works better.
> 
> >Brian, where did you learn about the ".seconds". And the .year, .month,
> >.day of
> 
> What's funny, is that after I'd gone to bed that night, I suddenly
> woke up and thought "Hang on, what about the attribute .seconds of a
> datetime object? I better check that out."
> 
> Which I promptly forgot about until your last reply.
> 
> Heh.
> 
> Liam Clarke
> 
> 
> 
> On Tue, 07 Dec 2004 09:04:46 -0800, Dick Moores <rdm at rcblue.com> wrote:
> > Brian van den Broek wrote at 07:50 12/7/2004:
> >
> >
> > >Dick Moores said unto the world upon 2004-12-07 07:04:
> > >>To Liam and Brian,
> > >
> > ><SNIP>
> > >
> > >>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=====================
> > >
> > ><SNIP>
> > >
> > >>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
> > >
> > >Oh Sir, you flatter me! My general knowledge of OOP is about the same as
> > >my general knowledge of the migration patterns of Siberian water-fowl. ;-)
> > >
> > >After Anna, Gonçalo, and Liam encouraged me to explore the datetime
> > >module, I read (OK, skimmed) the docs. For my original purpose, I just
> > >needed to test two times with '>', but I recalled something about
> > >timedelta objects being returned by subtracting one datetime from
> > >another. I hadn't used them before I wrote my script in reply to you. I
> > >learned about the handy for your purposes .seconds attribute in the
> > >Library Reference -- 6.10.2 timedelta Objects. (That's the section name
> > >in the 2.4 installed docs; I'm typing off line so can't [OK, won't] get
> > >a link.)
> > >
> > >>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.
> > >
> > >I'm not sure I do either :-)
> > >
> > >Here's why I did it:
> > >
> > >I discovered that in order to get the right "seconds until alarm" value
> > >from the datetime for now and the alarm datetime by subtracting one
> > >datetime object from another, I needed the alarm datetime to be in the
> > >future. But, since you can set an alarm for 09:00 tomorrow at 22:00
> > >today, I needed the alarm datetime to not use today's date. (If you use
> > >today's, you end up with seconds *since* 09:00 this morning, not the
> > >desired seconds *until* 09:00 tomorrow morning.) Since
> > >timedelta_object.seconds discards all difference save the seconds save
> > >those from the hours, minutes, and seconds difference in the two
> > >datetime objects, it doesn't matter what date the alarm datetime is set
> > >to. (The day information is in timedelta_object.days.)
> > >
> > >Or, so I thought. I'd first tried getting the alarm datetime by simply
> > >taking the date component of datetime.datetime.now() and adding to the
> > >day value. That works fine, provided you are not on the last day of the
> > >month. But, when checking boundary cases before posting the code I sent,
> > >I discovered this sort of thing:
> > >
> > > >>> last_day_of_june = datetime.datetime(2004, 6, 30) # long for clarity
> > > >>> ldj = last_day_of_june                            # short for typing
> > > >>> new_day = datetime.datetime(ldj.year, ldj.month, ldj.day + 1)
> > >
> > >Traceback (most recent call last):
> > >   File "<pyshell#8>", line 1, in -toplevel-
> > >     new_day = datetime.datetime(ldj.year, ldj.month, ldj.day + 1)
> > >ValueError: day is out of range for month
> > > >>>
> > >
> > >So, adding to the day or the month was out, unless I wanted elaborate
> > >code to determine which to add to under what circumstances. So then I
> > >thought, "Well, just change from now.day + 1 to now.year + 1, and all
> > >problems go away". And then I thought "Ah, but what if I try to run the
> > >script on Feb. 29?
> > >
> > >So, that's why I used now.year + 4. It still leaves open the possibility
> > >of getting bit by the every so often further correction to the calender,
> > >but, I *believe* the next one is due in 2100, so I think I can live with
> > >it. ;-)
> > >
> > >I'm not saying it isn't hackish, though. Better ways surely exist.
> >
> > Brian,
> >
> > I just can't succeed in reproducing the problems with the boundary
> > cases  with the +4 removed. I've tried setting my computer's clock to Nov
> > 30, Feb 29, 2004, and Dec 31, 2004 (without changing the time). It's now
> > about 9 am where I am, and setting the alarm time to 0700 (i.e., on the
> > next day) works fine in all 3 cases. And of course no problem at today's
> > date with setting the alarm time to 07:00.
> >
> > Isn't that what that note (1) on
> > <http://docs.python.org/lib/datetime-date.html> implies?
> >
> > I did the testing with <http://www.rcblue.com/Python/timer4-ForWeb.py>,
> > by remarking out the time.sleep() line and noting what the
> > print_hms(sleepSeconds) function prints.
> >
> > Dick
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> 
> --
> 'There is only one basic human right, and that is to do as you damn well please.
> And with it comes the only ba.>sic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list