开发者

Is this a bug in the Ruby time class when doing relative date arithmetic?

开发者 https://www.devze.com 2023-04-01 00:04 出处:网络
Can someone tell me if this is a bug in the ruby time class? ruby-开发者_C百科1.8.7-p334 :021 > now = Time.now

Can someone tell me if this is a bug in the ruby time class?

ruby-开发者_C百科1.8.7-p334 :021 > now = Time.now
 => Mon Aug 29 03:32:25 -0700 2011 
ruby-1.8.7-p334 :022 > raise "This should not fail" if (now + 1.day != now + 1.day.to_i)
RuntimeError: This should not fail
    from (irb):22
ruby-1.8.7-p334 :023 > 

As you can see I am getting a runtime error and I do not believe that I should be. I recently upgraded the active_support gem which I believe provides this functionality.

Thank you.

** UPDATE **

And, now it works, without any changes other than me going to bed and waking up and rerunning things. This is very strange; The snippet I provided above was directly cut-and-pasted from my terminal window.... I was running against 3.0.10 of activerecord/support/model/etc

Thanks to all for your thoughts on this matter!


While time.to_s does not include it, a Time object contains milliseconds - and not only that, it contains fractional seconds (with much higher resolution) (see: Time#subsec).

Time.now == Time.now will already be false, because each call to now will take several CPU ticks to complete. Also take a look at Time#eql?.

Return true if time and other_time are both Time objects with the same seconds and fractional seconds.


Surely it's not a bug in the Time class, because the difference seems to be in the day method of the Fixnum class. Moreover, that is not a method of the original Fixnum class; it shall be defined in some file you required before.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号