From: Morton Goldberg Date: 2006-10-09T23:15:32+09:00 Subject: Re: Beginning of day given a Time value 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