On Aug 27, 2010, at 2:52 PM, Glenn Little wrote:

My problem is that for mysql DateTime fields, the value ends up a
string that looks like

"2010-08-20 00:00:00"

But I want to do things like:

if (value < Time.now)....

My question is, what is the accepted/best-practice way to deal with
this?  I can cobble up a strftime string to convert a ruby Time to a
mysql string and let the comparison just be a string compare:

if (value < Time.now.strftime("%Y-%m-%d %H:%M:%S")....

But is that really the best way?  Feels a little clunky somehow and
something bothers me about doing a raw string compare, but it's still
a one-liner (and I could hide it in a helper function or add it to the
Time class even) so maybe I should just accept it. :-)


I have a boatload of Time extensions, and I would pretty much end up doing what you're doing except I have:

class Time
 def as_iso_date_time
   return self.strftime("%Y-%m-%d %H:%M:%S")
 end
// and dozens of other conversion and calculation methods
end

Then my code can read

if (value < Time.now.as_iso_date_time)

-- gw


--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to