Skip to content

Commit 1efa532

Browse files
committed
[GR-20446] Implement date as C extension
PullRequest: truffleruby/2693
2 parents 89755ec + 90712b5 commit 1efa532

31 files changed

+15925
-1929
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Compatibility:
3636
* Implemented `Enumerator::Lazy#with_index` (#2356).
3737
* Implement `rb_backref_set`.
3838
* Fix `Float#<=>` when comparing `Infinity` to other `#infinite?` values.
39+
* Implement `date` library as a C extension to improve compatibility (#2344).
3940

4041
Performance:
4142

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2
1+
3

lib/mri/date.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
# date.rb: Written by Tadayoshi Funaba 1998-2011
3+
4+
require 'date_core'
5+
6+
class Date
7+
8+
def infinite?
9+
false
10+
end
11+
12+
class Infinity < Numeric # :nodoc:
13+
14+
include Comparable
15+
16+
def initialize(d=1) @d = d <=> 0 end
17+
18+
def d() @d end
19+
20+
protected :d
21+
22+
def zero?() false end
23+
def finite?() false end
24+
def infinite?() d.nonzero? end
25+
def nan?() d.zero? end
26+
27+
def abs() self.class.new end
28+
29+
def -@() self.class.new(-d) end
30+
def +@() self.class.new(+d) end
31+
32+
def <=>(other)
33+
case other
34+
when Infinity; return d <=> other.d
35+
when Numeric; return d
36+
else
37+
begin
38+
l, r = other.coerce(self)
39+
return l <=> r
40+
rescue NoMethodError
41+
end
42+
end
43+
nil
44+
end
45+
46+
def coerce(other)
47+
case other
48+
when Numeric; return -d, d
49+
else
50+
super
51+
end
52+
end
53+
54+
def to_f
55+
return 0 if @d == 0
56+
if @d > 0
57+
Float::INFINITY
58+
else
59+
-Float::INFINITY
60+
end
61+
end
62+
63+
end
64+
65+
end

0 commit comments

Comments
 (0)