Skip to content

Instantly share code, notes, and snippets.

@wiggly
Last active December 25, 2015 15:28
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 wiggly/6997961 to your computer and use it in GitHub Desktop.
Save wiggly/6997961 to your computer and use it in GitHub Desktop.
Minimal test showing that adding a mixin to Time can cause incorrect results with ActiveSupport::TimeWithZone
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require APP_PATH
Rails.application.require_environment!
require 'timecop'
module BeforeAfterComparator
def mbefore?(other)
(self <=> other) == -1
end
def mafter?(other)
(self <=> other) == 1
end
end
def fbefore?(t1,t2)
(t1 <=> t2) == -1
end
def fafter?(t1,t2)
(t1 <=> t2) == 1
end
def time_info( t, label = 'current')
"#{label} - sec: #{t.to_i} - inspect: #{t.inspect} - class: #{t.class} - utc: #{t.utc?} - id: #{t.object_id}"
end
def main
Timecop.freeze do
london = Time.find_zone('London').now
sydney = Time.find_zone('Sydney').now
puts time_info(london,'london')
puts time_info(sydney,'sydney')
puts "Adding BeforeAfterComparator to Time\n\n"
Time.send :include, BeforeAfterComparator
puts "functions"
puts "sydney func before london : #{fbefore?(sydney, london)}"
puts "sydney func after london : #{fafter?(sydney, london)}"
puts "\n"
puts "methods"
puts "sydney method before london : #{sydney.mbefore?(london)}"
puts "sydney method after london : #{sydney.mafter?(london)}"
puts "\n"
puts "functions"
puts "sydney func before sydney : #{fbefore?(sydney, sydney)}"
puts "sydney func after sydney : #{fafter?(sydney, sydney)}"
puts "\n"
puts "methods"
puts "sydney method before sydney : #{sydney.mbefore?(sydney)}"
puts "sydney method after sydney : #{sydney.mafter?(sydney)}"
puts "\n"
puts "Adding BeforeAfterComparator to ActiveSupport::TimeWithZone\n"
ActiveSupport::TimeWithZone.send :include, BeforeAfterComparator
puts "functions"
puts "sydney func before london : #{fbefore?(sydney, london)}"
puts "sydney func after london : #{fafter?(sydney, london)}"
puts "\n"
puts "methods"
puts "sydney method before london : #{sydney.mbefore?(london)}"
puts "sydney method after london : #{sydney.mafter?(london)}"
puts "\n"
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment