Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize Date#<=> for comparing with other Date instances.
Browse files Browse the repository at this point in the history
See #3841.
headius committed Aug 22, 2016
1 parent 2d6762e commit 0c53b64
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/ruby/stdlib/date.rb
Original file line number Diff line number Diff line change
@@ -1402,14 +1402,22 @@ def - (x)
# two DateTime instances. When comparing a DateTime instance
# with a Date instance, the time of the latter will be
# considered as falling on midnight UTC.
class org::joda::time::DateTime
java_alias :compareDT, :compareTo, [org.joda.time.ReadableInstant]
end
def <=> (other)
case other
when Numeric
ajd <=> other
when Date
if other.kind_of?(Date)
# The method compareTo doesn't compare the sub milliseconds so after compare the two dates
# then we have to compare the sub milliseconds to make sure that both are exactly equal.
@dt.compareTo(other.dt).nonzero? || @sub_millis <=> other.sub_millis
@dt.compareDT(other.dt).nonzero? || @sub_millis <=> other.sub_millis
else
__internal_cmp(other)
end
end

private def __internal_cmp(other)
if other.kind_of? Numeric
ajd <=> other
else
begin
l, r = other.coerce(self)

0 comments on commit 0c53b64

Please sign in to comment.