Skip to content

Commit

Permalink
Merge pull request #37392 from cfitz25/isless-CompoundPeriod
Browse files Browse the repository at this point in the history
isless for CompoundPeriod and Period combinations
  • Loading branch information
quinnj authored Oct 16, 2020
2 parents 1dd89ec + daab24b commit 995002a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stdlib/Dates/src/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ end
Base.isless(x::FixedPeriod, y::OtherPeriod) = throw(MethodError(isless, (x, y)))
Base.isless(x::OtherPeriod, y::FixedPeriod) = throw(MethodError(isless, (x, y)))

Base.isless(x::Period, y::CompoundPeriod) = CompoundPeriod(x) < y
Base.isless(x::CompoundPeriod, y::Period) = x < CompoundPeriod(y)
Base.isless(x::CompoundPeriod, y::CompoundPeriod) = tons(x) < tons(y)
# truncating conversions to milliseconds, nanoseconds and days:
# overflow can happen for periods longer than ~300,000 years
toms(c::Nanosecond) = div(value(c), 1000000)
Expand Down
14 changes: 14 additions & 0 deletions stdlib/Dates/test/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,5 +504,19 @@ end
@test Dates.toms(Dates.Second(1) + Dates.Microsecond(1)) == 1e3
end

@testset "CompoundPeriod and Period isless()" begin
#tests for allowed comparisons
#FixedPeriod
@test (h - ms < h + ns) == true
@test (h + ns < h -ms) == false
@test (h < h -ms) == false
@test (h-ms < h) == true
#OtherPeriod
@test (2y-m < 25m+1y) == true
@test (2y < 25m+1y) == true
@test (25m+1y < 2y) == false
#Test combined Fixed and Other Periods
@test (1m + 1d < 1m + 1s) == false
end
end

0 comments on commit 995002a

Please sign in to comment.