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_yesterday = today.to_i - ONE_DAY_IN_SECS     yesterday = time_yesterday )     return "#{yesterday.year}-#{yesterday.month_with_leading_zero}- #{yesterday.day_with_leading_zero} "   end

# ... etc


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(     expected = "2008-05-04"     assert( expected == today)   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 method `parse' 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:in `send'         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:in `each'         from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/specification.rb: 412:in `initialize'         from (eval):1:in `new'         from (eval):1:in `load_specification'         from C:/ruby/lib/ruby/site_ruby/1.8/rubygems/source_index.rb: 128:in `eval'          ... 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:in `searcher'         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?


I managed to solve my problem using the Chronic library.

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… = This time yesterday

Therefore, = Midnight, yesterday.

yep, your logic is sound.

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