Most of the time-modification methods in ActiveSupport use Time#change
which
boils down to instantiating a new Time object.
If Time#change
is passed a null-change hash, e.g. {}
or { :month => 0 }
,
then the resulting Time object should compare equally to the original.
This appears to be the case in Mac OS X (Ruby 1.9.3-p0 and 1.9.2-p290) but does not
appear to be the case in Linux (Ruby 1.9.2-p290). The inequality is because the Time
object holds sub-usec precision, while Time#usec
returns a Fixnum representation.
However, that doesn't explain the different in behaviour between Mac OS X and Linux.
To gather more information, please comment below with the output of these commands.
The output from ruby should be 0.0
, but in Linux small floats like 2.67e-07
occur.
ruby --version; \
uname -a; \
ruby -e "t = Time.now; p Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec, t.usec) - t"
Cheers!
Paul
@d11wtq points out that this can be achieved by passing Rational(t.nsec, 1000)
instead of t.usec
to Time.local
. However, Time#nsec
doesn't exist in Ruby 1.8.7 which is still supported by ActiveSupport. Less importantly, Rational
needs to be loaded from stdlib in Ruby 1.8.7, while it's provided by core in Ruby 1.9.