简体   繁体   中英

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. But after switching to ruby 1.9.2 -p290 its not working. 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.

Thanks

you can use

Time.now.asctime

in 1.9

EDIT 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:

Ok, I got your question somewhat wrong. The above patch will cause Time.now to return the string (not the Time object).

If you just want to see Time object to be represented different you could apply this:

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

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