From: ara.t.howard@... Date: 2006-10-09T23:50:29+09:00 Subject: Re: Beginning of day given a Time value On Mon, 9 Oct 2006, 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 > > Thanks, > Michael Guterl why are you using parse? without it your method is straightforward enough: harp:~ > cat a.rb require "test/unit" class Time def begining_of_day Time.mktime(year, month, day).send(gmt? ? :gmt : :localtime) end end class TestBeginOfDay < Test::Unit::TestCase def test_begin_of_day start_of_day = Time.mktime 2006, 10, 1 time = Time.mktime 2006, 10, 1, 9, 45 assert_equal start_of_day, time.begining_of_day end end harp:~ > ruby a.rb Loaded suite a Started . Finished in 0.000914 seconds. 1 tests, 1 assertions, 0 failures, 0 errors regards. -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei