简体   繁体   English

Ruby Enterprise Edition以错误的格式提供Time.now

[英]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. 在我的VPS(Ubuntu 10.04LTS)上,我具有通过RVM安装的ree-1.8.7-2011.03和ruby-1.9.2-p180。 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 . 我的问题是,当我在ree-1.8.7(irb)中调用Time.now时,我得到Thu May 12 12:16:50 +0200 2011 ,而在ruby-1.9.2(irb)中也是如此得到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). 问题是日期的ree版本在我的rails查询中不可用(生成的SQL简直是坏掉的)。 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. 目前,在每个查询中都不能使用strftime格式化时间,而且都不能切换到1.9.2,因此我需要您的帮助来弄清楚为什么会发生并修复它。

Thanks for any help! 谢谢你的帮助!

This is not a REE issue. 这不是REE问题。 Ruby 1.9.2 changes the default format for Time#to_s. Ruby 1.9.2更改了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. 最好不要使用默认的Time#to_s格式,而是始终使用自定义帮助程序或方法格式化日期输出,这是一个好习惯,否则您将无法控制信息的显示方式。

Formatting the time using strftime in every single query is not an option at the moment 目前无法在每个查询中使用strftime格式化时间

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. 临时的解决方法是重写Ruby 1.8.7 Time#to_s方法以使用自定义格式。 However, making such this change might break other libraries. 但是,进行此更改可能会破坏其他库。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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