在进行相对日期算术时,这是Ruby时间类中的错误吗?

时间:2022-11-04 01:57:24

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

有人能告诉我这是否是红宝石时间课中的错误?

ruby-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.

正如您所看到的,我遇到了运行时错误,我不相信我应该这样做。我最近升级了active_support gem,我相信它提供了这个功能。

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

并且,现在它可以工作,除了我睡觉,醒来和重新运行之外没有任何变化。这很奇怪;我上面提供的代码片段直接从我的终端窗口剪切粘贴....我正在运行3.0.10的activerecord / support / model / etc

Thanks to all for your thoughts on this matter!

感谢大家对此事的想法!

3 个解决方案

#1


3  

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.to_s不包含它,但Time对象包含毫秒 - 不仅如此,它还包含小数秒(具有更高的分辨率)(请参阅: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?.

Time.now == Time.now已经是假的,因为现在每次调用都需要几个CPU滴答才能完成。还看看Time#eql?。

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

如果time和other_time都是具有相同秒和小数秒的Time对象,则返回true。

#2


0  

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.

当然,它不是Time类中的错误,因为差异似乎是Fixnum类的day方法。而且,这不是原始Fixnum类的方法;它应在您之前需要的某个文件中定义。

#3


0  

What @marcel-jackwerth says is correct. Plug this in if you want to see them equal the same (since there's a very high likelihood that the calls will be processed in the same second):

@ marcel-jackwerth所说的是正确的。如果你想看到它们相同(因为很有可能在同一秒内处理呼叫),请将其插入:

ruby-1.8.7-p330 :021 > (Time.now + 1.day).to_i == (Time.now + 1.day).to_i
=> true 

#1


3  

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.to_s不包含它,但Time对象包含毫秒 - 不仅如此,它还包含小数秒(具有更高的分辨率)(请参阅: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?.

Time.now == Time.now已经是假的,因为现在每次调用都需要几个CPU滴答才能完成。还看看Time#eql?。

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

如果time和other_time都是具有相同秒和小数秒的Time对象,则返回true。

#2


0  

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.

当然,它不是Time类中的错误,因为差异似乎是Fixnum类的day方法。而且,这不是原始Fixnum类的方法;它应在您之前需要的某个文件中定义。

#3


0  

What @marcel-jackwerth says is correct. Plug this in if you want to see them equal the same (since there's a very high likelihood that the calls will be processed in the same second):

@ marcel-jackwerth所说的是正确的。如果你想看到它们相同(因为很有可能在同一秒内处理呼叫),请将其插入:

ruby-1.8.7-p330 :021 > (Time.now + 1.day).to_i == (Time.now + 1.day).to_i
=> true