My MySQL is storing date time stamps in GMT, though server’s time is in EST. I convert my date time to local (server’s datetime EST) before adding to MySQL but it stores it GMt instead of EST. What is wrong here?
item.update_attributes({:request_datetime => Time.parse(date+" "+time+" "+timezone).localtime}) # this becomes EST
puts Time.parse(date+" "+time+" "+timezone).localtime.to_s # prints correct time zone, EST.
mysql> SELECT request_datetime,NOW(),TIMESTAMPDIFF(MINUTE,request_datetime,NOW()) FROM items Where item='2542';
+---------------------+---------------------+----------------------------------------------+
| request_datetime | NOW() | TIMESTAMPDIFF(MINUTE,request_datetime,NOW()) |
+---------------------+---------------------+----------------------------------------------+
| 2012-05-05 22:30:02 | 2012-05-05 18:30:05 | -239 |
+---------------------+---------------------+----------------------------------------------+
1 row in set (0.00 sec)
mysql>
Rails stores all time in UTC/GMT by default, and converts it to local time as necessary, this makes it easier to get correct times irrespective of your server or to allow you to establish times on a per user basis.