diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index 83641785a85ef..8d81262af19ec 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -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) diff --git a/stdlib/Dates/test/periods.jl b/stdlib/Dates/test/periods.jl index 4f28b1da85af0..f4dd65d0efd2a 100644 --- a/stdlib/Dates/test/periods.jl +++ b/stdlib/Dates/test/periods.jl @@ -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