Opening the Time Class

I’m trying to add some extra methods to the time class by opening it
up like this:

class Time # opening up ruby time

ONE_DAY_IN_SECS = 86400

def self.yesterday_at_midnight(today = Time.now)
time_yesterday = today.to_i - ONE_DAY_IN_SECS
yesterday = Time.at( time_yesterday )
return "#{yesterday.year}-#{yesterday.month_with_leading_zero}-
#{yesterday.day_with_leading_zero} "
end

… etc

end

When I run my test from within NetBeans 6.0.1…

require ‘test/unit’

class TimeTest < Test::Unit::TestCase

MONDAY_05_05_2008 = 1209972973

def test_can_open_time_to_add_yesterday_at_midnight
today = Time.yesterday_at_midnight(Time.at(MONDAY_05_05_2008))
expected = “2008-05-04”
assert( expected == today)
end

end

This works and I get a passing test.

However if I include an additional require line in my test: require
‘unit_test_helper’ then I get the following error:

C:/ruby/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:92:in
load_specification': undefined methodparse’ for Time:Class
(NoMethodError)
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
314:in date=' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb: 416:insend’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
416:in initialize' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb: 412:ineach’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb:
412:in initialize' from (eval):1:innew’
from (eval):1:in load_specification' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/source_index.rb: 128:ineval’
… 11 levels…
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:211:in
synchronize' from C:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:211:insearcher’
from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
30:in `require’
from C:/dev/esp/test/unit/models/time_test.rb:2

I think this has something to do with not requiring time properly, but
I can’t work it out. Has anyone has a similar problem?

Jules

I managed to solve my problem using the Chronic library.

http://chronic.rubyforge.org/

irb(main):002:0> require ‘chronic’
=> true
irb(main):003:0> Chronic.parse(‘yesterday’)
=> Tue May 06 12:00:00 +1000 2008
irb(main):004:0>

Um…

Time.now.yesterday = This time yesterday

Therefore,

Time.now.yesterday.beginning_of_day = Midnight, yesterday.

yep, your logic is sound.

On sqlite3, doing a select with the date ‘2008-05-07’ defaults to
midnight.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs