this is ruby quiz 144, anybody feel like doing it in CL?
Time Window (#144)
by Brian Candler
Write a Ruby class which can tell you whether the current time (or any
given time) is within a particular "time window". Time windows are
defined by strings in the following format:
ruby
# 0700-0900 # every day between these times
# Sat Sun # all day Sat and Sun, no other times
# Sat Sun 0700-0900 # 0700-0900 on Sat and Sun only
# Mon-Fri 0700-0900 # 0700-0900 on Monday to Friday only
# Mon-Fri 0700-0900; Sat Sun # ditto plus all day Sat and Sun
# Fri-Mon 0700-0900 # 0700-0900 on Fri Sat Sun Mon
# Sat 0700-0800; Sun 0800-0900 # 0700-0800 on Sat, plus 0800-0900 on Sun
Time ranges should exclude the upper bound, i.e. 0700-0900 is 07:00:00
to 08:59:59. An empty time window means "all times everyday". Here are
some test cases to make it clearer:
ruby
class TestTimeWindow < Test::Unit::TestCase
def test_window_1
w = TimeWindow.new("Sat-Sun; Mon Wed 0700-0900; Thu 0700-0900 1000-1200")
assert ! w.include?(Time.mktime(2007,9,25,8,0,0)) # Tue
assert w.include?(Time.mktime(2007,9,26,8,0,0)) # Wed
assert ! w.include?(Time.mktime(2007,9,26,11,0,0))
assert ! w.include?(Time.mktime(2007,9,27,6,59,59)) # Thu
assert w.include?(Time.mktime(2007,9,27,7,0,0))
assert w.include?(Time.mktime(2007,9,27,8,59,59))
assert ! w.include?(Time.mktime(2007,9,27,9,0,0))
assert w.include?(Time.mktime(2007,9,27,11,0,0))
assert w.include?(Time.mktime(2007,9,29,11,0,0)) # Sat
assert w.include?(Time.mktime(2007,9,29,0,0,0))
assert w.include?(Time.mktime(2007,9,29,23,59,59))
end
def test_window_2
w = TimeWindow.new("Fri-Mon")
assert ! w.include?(Time.mktime(2007,9,27)) # Thu
assert w.include?(Time.mktime(2007,9,28))
assert w.include?(Time.mktime(2007,9,29))
assert w.include?(Time.mktime(2007,9,30))
assert w.include?(Time.mktime(2007,10,1))
assert ! w.include?(Time.mktime(2007,10,2)) # Tue
end
def test_window_nil
w = RDS::TimeWindow.new("")
assert w.include?(Time.mktime(2007,9,25,1,2,3)) # all times
end
end
marc
--
······@sdf.lonestar.org
SDF Public Access UNIX System - http://sdf.lonestar.org
Ok, I did it. You can check it out here:
http://www.lispcast.com/index.php/2007/10/time-window-matcher/
I have the code, plus some comments on it.
Eric
http://www.lispcast.com
············@gmail.com wrote:
> Ok, I did it. You can check it out here:
>
> http://www.lispcast.com/index.php/2007/10/time-window-matcher/
>
> I have the code, plus some comments on it.
>
You requested feedback. Code looks great. Spotted this:
(some #'identity (mapcar (lambda (tw)
(includesp tw time))
(rest tw))))
I am scratching my head, why not?:
(some (lambda (tw) (includesp tw time)) (rest tw))
Also saves you from consing up all the results. You did the same later
with (every 'identity (lambda ....
My next point would be you do not really need the result (which is a
predicate anyway, not the matched tw) so find-if does as well and does
not mislead the maintainer into thinking the result matters other than
being non-nil.
Finally:
(loop for tw in (rest tw)
thereis (includesp tw time))
Not so many parentheses. :)
hth,kt
--
http://www.theoryyalgebra.com/
"Career highlights? I had two. I got an intentional walk
from Sandy Koufax and I got out of a rundown against the Mets."."
- Bob Uecker
Ken Tilton wrote:
>
>
> ············@gmail.com wrote:
>
>> Ok, I did it. You can check it out here:
>>
>> http://www.lispcast.com/index.php/2007/10/time-window-matcher/
>>
>> I have the code, plus some comments on it.
>>
>
>
> You requested feedback. Code looks great. Spotted this:
(defun hour-of (time)
(multiple-value-bind (second minute hour) (decode-universal-time time)
hour))
That would give me warnings about unused second and minute variables. I
could declare ignore or:
(nth-value 2 (decode....))
kt
--
http://www.theoryyalgebra.com/
"Career highlights? I had two. I got an intentional walk
from Sandy Koufax and I got out of a rundown against the Mets."."
- Bob Uecker