简体   繁体   English

来自ruby 1.8.7的ruby 1.9.2中的不同时间格式导致问题

[英]Different Time Format in ruby 1.9.2 from ruby 1.8.7 causing issues

I am upgrading the ruby version from ruby 1.8.7 to ruby 1.9.2 in my exiting Ruby on rails application which has extensive use of Time related calculations. 我正在退出的Ruby on rails应用程序中将ruby版本从ruby 1.8.7升级到ruby 1.9.2,该应用程序广泛使用与时间相关的计算。 But after switching to ruby 1.9.2 -p290 its not working. 但在切换到ruby 1.9.2 -p290之后它无法正常工作。 I guess the issue is with 我猜问题就在于此

$ 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 

Can some please tell me how to solve this kind of issue or How can change or override default ruby 1.9.2 format back to the old one or how to solve this time related changes in the newer version of ruby. 有些请告诉我如何解决这类问题或如何更改或覆盖默认的ruby 1.9.2格式回旧版或如何解决这个时间相关的更新版本的ruby。

Thanks 谢谢

you can use 您可以使用

Time.now.asctime

in 1.9 在1.9

EDIT 1: 编辑1:

well, depends on how you're going to use it. 好吧,取决于你将如何使用它。 This should probably work for you: 这应该适合你:

irb(main):001:0> class Time
irb(main):002:1> class << self
irb(main):003:2>   alias :orig_now :now
irb(main):004:2>
irb(main):005:2*   def now
irb(main):006:3>     orig_now.asctime
irb(main):007:3>   end
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> Time.now
=> "Thu Jul 28 12:29:08 2011"  # STRING
irb(main):011:0>

EDIT 2: 编辑2:

Ok, I got your question somewhat wrong. 好的,我的问题有些不对劲。 The above patch will cause Time.now to return the string (not the Time object). 上面的补丁将导致Time.now返回字符串(而不是Time对象)。

If you just want to see Time object to be represented different you could apply this: 如果您只想看到Time对象被表示为不同,您可以应用此:

irb(main):011:0> class Time
irb(main):012:1>
irb(main):013:1* def inspect
irb(main):014:2>   self.asctime
irb(main):015:2> end
irb(main):016:1> end
=> nil
irb(main):017:0> Time.now
=> Thu Jul 28 13:41:16 2011   # TIME

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

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