Skip to content

Instantly share code, notes, and snippets.

@avit
Last active December 17, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avit/5651344 to your computer and use it in GitHub Desktop.
Save avit/5651344 to your computer and use it in GitHub Desktop.
Problem with different level of precision when adding ActiveSupport::Duration than Fixnum seconds. It looks like Rails 4.0 has a fix for this. The bug (illustrated here with plain ruby) depends on the system environment for the precision error to show up.
require 'active_support/time'
require 'active_support/version'
t0 = Time.now # => 2013-05-26 01:14:51 +0000
t1 = t0 + 86400
t2 = t0 + 1.day
puts "Ruby %s / ActiveSupport %s" % [RUBY_VERSION, ActiveSupport::VERSION::STRING]
puts "Time + Fixnum nsec: %s" % t1.nsec # => 11111111
puts "Time + AS::Duration nsec: %s" % t2.nsec # => 11111000
t0 = Time.now
t1 = Time.local(t0.year, t0.month, t0.day, t0.hour, t0.min, t0.sec, t0.usec)
puts "Ruby %s" % RUBY_VERSION
puts "t0 == t1 is %s" % (t0 == t1)
puts "t0.nsec: %s" % t0.nsec
puts "t1.nsec: %s" % t1.nsec
[1] pry(#<RSpec::Core::ExampleGroup::Nested_4>)> (t0 + 86400).to_f
=> 1369615883.5234501
[2] pry(#<RSpec::Core::ExampleGroup::Nested_4>)> (t0 + 1.day).to_f
=> 1369615883.5234501
[30] pry(#<RSpec::Core::ExampleGroup::Nested_4>)> (t0 + 1.day).to_f
=> 1369615386.78719
[31] pry(#<RSpec::Core::ExampleGroup::Nested_4>)> (t0 + 86400).to_f
=> 1369615386.7871902
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment