Permalink
Browse files

Add Date::Infinity implementation, bump version to 1.0.9 (Fixes #50)

  • Loading branch information...
1 parent f3c1416 commit b0fcad6db2ac9740afe3058b391cfbc0675b1180 @jeremyevans committed Mar 19, 2013
Showing with 60 additions and 1 deletion.
  1. +4 −0 CHANGELOG
  2. +1 −1 home_run.gemspec
  3. +55 −0 lib/date.rb
View
@@ -1,3 +1,7 @@
+=== 1.0.9 (2013-03-19)
+
+* Add Date::Infinity implementation (jeremyevans) (#50)
+
=== 1.0.8 (2013-02-28)
* Switch %i to %d in format strings to work on ruby 2.0 (jeremyevans)
View
@@ -1,6 +1,6 @@
HOME_RUN_GEMSPEC = Gem::Specification.new do |s|
s.name = 'home_run'
- s.version = '1.0.8'
+ s.version = '1.0.9'
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG", "LICENSE"]
View
@@ -4,4 +4,59 @@
raise unless RUBY_PLATFORM =~ /mswin|mingw/
require "#{RUBY_VERSION[0...3]}/date_ext"
end
+
+class Date
+ class Infinity < Numeric # :nodoc:
+
+ include Comparable
+
+ def initialize(d=1) @d = d <=> 0 end
+
+ def d() @d end
+
+ protected :d
+
+ def zero? () false end
+ def finite? () false end
+ def infinite? () d.nonzero? end
+ def nan? () d.zero? end
+
+ def abs() self.class.new end
+
+ def -@ () self.class.new(-d) end
+ def +@ () self.class.new(+d) end
+
+ def <=> (other)
+ case other
+ when Infinity; return d <=> other.d
+ when Numeric; return d
+ else
+ begin
+ l, r = other.coerce(self)
+ return l <=> r
+ rescue NoMethodError
+ end
+ end
+ nil
+ end
+
+ def coerce(other)
+ case other
+ when Numeric; return -d, d
+ else
+ super
+ end
+ end
+
+ def to_f
+ return 0 if @d == 0
+ if @d > 0
+ Float::INFINITY
+ else
+ -Float::INFINITY
+ end
+ end
+ end
+end
+
require "date/format" unless defined?(Date::Format::ZONES)

0 comments on commit b0fcad6

Please sign in to comment.