简体   繁体   中英

Ruby Enterprise Edition giving Time.now in wrong format

On my VPS (Ubuntu 10.04LTS), I have ree-1.8.7-2011.03 and ruby-1.9.2-p180 installed through RVM. My problem is that when I call Time.now in ree-1.8.7(irb), I get Thu May 12 12:16:50 +0200 2011 , when I do the same in ruby-1.9.2(irb), I get 2011-05-12 12:17:44 +0200 .

The problem is the ree version of the date is unusable in my rails queries (The SQL generated is just plain broken). Formatting the time using strftime in every single query is not an option at the moment, and neither is switching to 1.9.2, so I need your help to figure out why this is happening and fix it.

Thanks for any help!

This is not a REE issue. Ruby 1.9.2 changes the default format for Time#to_s.

$ rvm use 1.8.7
ruby-1.8.7-p334 :001 > Time.now
# => Thu May 12 12:42:35 +0200 2011 

$ rvm use 1.9.2
ruby-1.9.2-p180 :001 > Time.now
# => 2011-05-12 12:42:44 +0200 

It's a good practice to never rely on the default Time#to_s format but always use a custom helper or method to format a date output otherwise you have no control on how the information are displayed.

Formatting the time using strftime in every single query is not an option at the moment

Not only it should be an option, it should have been your first choice. I strongly encourage you to fix the existing code to use a custom formatting method.

A temporary workaround would be to override Ruby 1.8.7 Time#to_s method to use a custom format. However, making such this change might break other libraries.

在config / initializers / app.rb中怎么样

Time::DATE_FORMATS[:default] = "Your preferred date format"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM