Created
March 9, 2013 07:44
-
-
Save matpowel/5123358 to your computer and use it in GitHub Desktop.
Monkey patch for broken Time.at in JRuby 1.7.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if RUBY_PLATFORM =~ /java/i | |
# ruby-units monkey patches the Time class which in turn exposes a bug in | |
# JRuby (https://github.com/jruby/jruby/issues/565) | |
class Time | |
class << self | |
if defined?( Unit ) | |
# ruby units got there first | |
alias agworld_ruby_time_at unit_time_at | |
alias agworld_unit_time_at at | |
else | |
alias agworld_ruby_time_at at | |
end | |
end | |
def self.at(arg, ms=nil) | |
if defined?( Unit ) | |
# Unit existing implies ruby-units is loaded | |
if arg.kind_of?( Unit ) | |
# call ruby-units' monkey patched Time.at, safe to pass through ms | |
# because ruby-units internally passes through to JRuby's Time.at with | |
# a float as the first arg and not a Time (which avoids the bug) | |
return agworld_unit_time_at( arg, ms ) | |
end | |
end | |
# otherwise always call to Ruby's original Time.at | |
if ms.nil? | |
return agworld_ruby_time_at(arg) | |
else | |
return agworld_ruby_time_at(arg + (ms*0.000001)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment