Basically I am using the MySQL gem for Ruby, and I have no reasonable support for date comparison. The Mysql::Time
class only gives me only accessor methods like year, month, second, etc. I could do much better date comparison, if I could turn this into a Ruby DateTime object. How can convert MySQL's DateTime field to a Julian day number which can be passed to DateTime.jd
?
views:
50answers:
3
+1
A:
Consider using Ruby/DBI instead of using the MySQL gem directly. Ruby/DBI should take care of the conversion into standard Ruby classes for you automatically, and as an added bonus feature if you ever change the DBMS you're running, your use of the DBI doesn't change.
Ken Bloom
2010-07-04 03:44:18
I take it this still uses the mysql gem?
Zombies
2010-07-04 12:56:22
@Zombies, Yes. Under the hood, it uses the MySQL gem to actually talk to MySQL.
Ken Bloom
2010-07-04 13:08:22
Which one to choose as accepted answer??
Zombies
2010-07-07 20:17:45
@Zombies: whichever one you actually used.
Ken Bloom
2010-07-07 22:12:13
+1
A:
You could use MySQL's TO_DAYS
function to get the date as an integer number of days since the year zero (and just add the appropriate offset to have a Julian Day number), or you could use the UNIX_TIMESTAMP
function to get an integer number of seconds since 1970-01-01 00:00:00 UTC.
Ken Bloom
2010-07-04 03:48:44
+1
A:
class Mysql::Time
def to_datetime
DateTime.civil(year,month,day,hour,minute,second)
end
end
Ken Bloom
2010-07-04 03:54:47