0

I love ruby's magic, but every so often you run across something that seems like a no-brainer. Here's my problem:

I'm storing a value as Time in my DB using MySQL's Time Column type. This store just the hh:mm:ss values.

Later when doing comparison's Ruby will grab this value and append 2001-01-01 hh:mm:ss UTC to it. (Record1 below was taken from my DB and is an ActiveRecord object obtained from a collection that happens to have a time attribute stored using MYSQL's Time function)

So it appears that my only option for useful comparions's is to use strftime? Is that right?

    (ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0])

    ruby-1.9.2-p290 :009 > record1.time
     => 2000-01-01 08:30:00 UTC 

    ruby-1.9.2-p290 :001 > time1 = "08:30:00"
     => "08:30:00" 
    ruby-1.9.2-p290 :002 > @time_obj1 = Time.parse time1
     => 2012-01-27 08:30:00 -0700 
    ruby-1.9.2-p290 :003 > @time_obj1.to_s
     => "2012-01-27 08:30:00 -0700" 
    ruby-1.9.2-p290 :004 >
Ashley Raiteri
  • 700
  • 8
  • 17
  • Repeat Q - http://stackoverflow.com/questions/6359978/represent-a-time-with-no-date-in-ruby – gef Jan 27 '12 at 21:14
  • Ruby's Time includes dates because it's based on the underlying OS's idea of Time, which is a DateTime equivalent. There is no tie to a database's idea of a Timestamp or a specific field type that is a Time value. Perhaps ActiveRecord is misunderstanding the field type? – the Tin Man Jan 27 '12 at 22:22

1 Answers1

1

So it appears that my only option for useful comparions's is to use strftime? Is that right?

That, or you could do something like

timestring = "#{@time_obj1.hour}:#{@time_obj1.min}:#{@time_obj1.sec}"

But strftime is quite a few less characters, and the more standard way to achieve what you're after.

michaelmichael
  • 13,755
  • 7
  • 54
  • 60