Telling the time

Discussion of Common Lisp

Telling the time

Postby I X Code X 1 » Fri Jul 08, 2011 10:14 am

Okay, I am playing around with getting the time in Common Lisp.

Code: Select all
(encode-universal-time 6 22 19 25 1 2002)


This encodes a calendar time into universal time. It's somewhat readable, but was wondering if there is anyway to just enter an hour? I am trying to make a very simplistic representation of an alarm clock and would love to be able to somehow type in:

(set-alarm 3) ; this would tell the alarm clock to go off at the next time it is 3:00. I would be willing to use military time if that is the only thing available to Common Lisp.

I'm assuming I might have to write my own function for this, but not sure how to go about doing this. Also, any way to make lisp make a noise? :lol:
User avatar
I X Code X 1
 
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

Re: Telling the time

Postby I X Code X 1 » Fri Jul 08, 2011 10:38 am

I've written this:

Code: Select all
(defun set-alarm (m h d mo y)
  (loop if (equal (encode-universal-time 0 m h d mo y)
                  (get-universal-time))
      return "wake up!"))


However, I would like to just have the user input minute and hour. Is there any functions like this: get-current-day, get-current-month, get-current-year?

Also, where the return is I would like it to make a noise.
User avatar
I X Code X 1
 
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

Re: Telling the time

Postby Ramarren » Fri Jul 08, 2011 10:42 am

There is a library called chronicity (available through quicklisp) which allows parsing descriptive time strings. It uses local-time library for time management, which I believe is the usual for more complex time manipulation. I have never written code which significantly involved time, so I don't really have experience with those.

I X Code X 1 wrote:Also, any way to make lisp make a noise?


Making computer make a noise is not really programming language specific. The easiest way is probably to use SDL. There are bindings to SDL and its subprojects provided by lispbuilder- series of libraries.

I X Code X 1 wrote: Is there any functions like this


You can combine get-universal-time with decode-universal-time.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Telling the time

Postby I X Code X 1 » Fri Jul 08, 2011 10:49 am

Ramarren wrote:There is a library called chronicity (available through quicklisp) which allows parsing descriptive time strings. It uses local-time library for time management, which I believe is the usual for more complex time manipulation. I have never written code which significantly involved time, so I don't really have experience with those.


Thanks, looking into Chronicity right now. Think it might be exactly what I need!

EDIT:

Code: Select all
(defun set-alarm (h m)
  (loop if (equal
            (encode-universal-time 0 m h (chronicity:day-of *)
                                   (chronicity:month-of *)
                                   (chronicity:year-of *))
                  (get-universal-time))
      return "wake up!"))


This is my working rendition of an alarm clock. All it does not do now is make a noise. Good luck waking up from that :lol:
User avatar
I X Code X 1
 
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

Re: Telling the time

Postby Paul » Fri Jul 08, 2011 5:07 pm

I X Code X 1 wrote:I've written this:

Code: Select all
(defun set-alarm (m h d mo y)
  (loop if (equal (encode-universal-time 0 m h d mo y)
                  (get-universal-time))
      return "wake up!"))


However, I would like to just have the user input minute and hour. Is there any functions like this: get-current-day, get-current-month, get-current-year?

Also, where the return is I would like it to make a noise.


Do something like this:
Code: Select all
(defvar *alarm-time*)
(defun set-alarm (h &optional (m 0))
  (multiple-value-bind (sc mn hr dy mo yr) (get-decoded-time)
    (declare (ignore sc))
    (assert (or (> h hr) (and (= h hr) (> m mn))))
    (setq *alarm-time* (encode-universal-time 0 m h dy mo yr))))

(defun check-alarm ()
  (when (and *alarm-time* (<= *alarm-time* (get-universal-time)))
    (setq *alarm-time* nil)
    #| make a noise |#))


You can do check-alarm in a loop if you must, but there's probably a better way: at least put a (sleep 1) in the loop as well. For the "make-a-noise" thing, you'll have to go outside Lisp (either FFI or run another program. If you just want a beep, try (apropos "feep") and (apropos "beep") for possibilities.)
Paul
 
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: Telling the time

Postby I X Code X 1 » Fri Jul 08, 2011 10:09 pm

Thanks for the Suggestion, Paul! I ended up just using the beep. Not a very effective alarm clock, but I just wanted to see if I could make one that actually does function. Mission accomplished! :D
User avatar
I X Code X 1
 
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY

Re: Telling the time

Postby Konfusius » Sat Jul 09, 2011 2:35 am

if you want to get the time that is 3 hours in the future, you can do this by

Code: Select all
(+ (get-universal-time) 10800))

where 10600 is the number of seconds that 3 hours have.
Konfusius
 
Posts: 62
Joined: Fri Jun 10, 2011 6:38 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 1 guest

cron