Morton Goldberg
10/9/2006 2:16:00 PM
On Oct 9, 2006, at 9:45 AM, Michael Guterl wrote:
> Anyone have a nice idiomatic way given an instance of Time, to
> return the
> beginning of that day? Below is my approach but it just feels very
> wrong...
>
> require "test/unit"
> require "time"
>
> class Time
> def begin_of_day
> Time.parse "#{self.month}/#{self.day}/#{self.year}"
> end
> end
>
> class TestBeginOfDay < Test::Unit::TestCase
> def test_begin_of_day
> start_of_day = Time.parse "10/1/2006 00:00"
> time = Time.parse "10/1/2006 9:45"
> assert_equal(start_of_day, time.begin_of_day)
> end
> end
How about this?
class Time
def begin_of_day
self - 3600 * hour - 60 * min - sec
end
end
It passes your unit test.
Regards, Morton