儘管服務器的時間在EST,但我的MySQL正在以GMT格式存儲日期時間戳。在添加到MySQL之前,我將日期時間轉換爲本地(服務器的日期時間EST),但它將GMt而不是EST存儲起來。這裏有什麼問題?儘管將日期時間轉換爲當地時間,但Rails將日期時間存儲爲GMT
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>
這是更好 - '選擇request_datetime,UTC_TIMESTAMP(),TIMESTAMPDIFF(MINUTE,request_datetime,UTC_TIMESTAMP())FROM項目如果項目= '2542';' –