From 5416a09cbda5fdd807e215faee4ab349c086a08c Mon Sep 17 00:00:00 2001 From: Nils Date: Wed, 26 Aug 2020 20:21:05 +0100 Subject: [PATCH 1/6] Add missing support for addition and subtraction Inspired by the discussion in #28570 --- stdlib/Dates/src/arithmetic.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index be712905188f1..b01aece4fa640 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -84,6 +84,12 @@ end (-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y))) (+)(y::Period, x::TimeType) = x + y +# Missing support +(+)(x::T, y::Missing) where {T<:AbstractTime} = missing +(+)(x::Missing, y::T) where {T<:AbstractTime} = missing +(-)(x::T, y::Missing) where {T<:AbstractTime} = missing +(-)(x::Missing, y::T) where {T<:AbstractTime} = missing + # AbstractArray{TimeType}, AbstractArray{TimeType} (-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = Vector(x) - Vector(y) (-)(x::AbstractRange{T}, y::AbstractRange{T}) where {T<:TimeType} = Vector(x) - Vector(y) From 6e44f90dcaba3cb9da1f8c652ca380a3e45a0a9d Mon Sep 17 00:00:00 2001 From: Nils Date: Wed, 26 Aug 2020 20:49:01 +0100 Subject: [PATCH 2/6] Update stdlib/Dates/src/arithmetic.jl Co-authored-by: Milan Bouchet-Valat --- stdlib/Dates/src/arithmetic.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index b01aece4fa640..ba7fcefb20a19 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -85,10 +85,10 @@ end (+)(y::Period, x::TimeType) = x + y # Missing support -(+)(x::T, y::Missing) where {T<:AbstractTime} = missing -(+)(x::Missing, y::T) where {T<:AbstractTime} = missing -(-)(x::T, y::Missing) where {T<:AbstractTime} = missing -(-)(x::Missing, y::T) where {T<:AbstractTime} = missing +(+)(x::AbstractTime, y::Missing) = missing +(+)(x::Missing, y::AbstractTime) = missing +(-)(x::AbstractTime, y::Missing) = missing +(-)(x::Missing, y::AbstractTime) = missing # AbstractArray{TimeType}, AbstractArray{TimeType} (-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = Vector(x) - Vector(y) From ffa2884d4b8007fc18f4fd0a3888790676dbbe5a Mon Sep 17 00:00:00 2001 From: Nils Date: Wed, 26 Aug 2020 20:59:58 +0100 Subject: [PATCH 3/6] Add tests for missing arithmetic These test the exported subtypes of `AbstractTime` --- stdlib/Dates/test/arithmetic.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index fe49b1127f9bf..6be02c2fa255a 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -499,4 +499,13 @@ end end end +@testset "Missing arithmetic" begin + for t ∈ [Date; Time; subtypes(DatePeriod); subtypes(TimePeriod)] + @test ismissing(t(1) + missing) + @test ismissing(missing + t(1)) + @test ismissing(t(1) - missing) + @test ismissing(missing - t(1)) + end +end + end From 6e7aea4e05ca70086d9aefe51c54e7c44c40d24a Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Wed, 26 Aug 2020 22:17:51 +0200 Subject: [PATCH 4/6] Remove trailing whitespace --- stdlib/Dates/src/arithmetic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index ba7fcefb20a19..6537f4e1caa82 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -89,7 +89,7 @@ end (+)(x::Missing, y::AbstractTime) = missing (-)(x::AbstractTime, y::Missing) = missing (-)(x::Missing, y::AbstractTime) = missing - + # AbstractArray{TimeType}, AbstractArray{TimeType} (-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = Vector(x) - Vector(y) (-)(x::AbstractRange{T}, y::AbstractRange{T}) where {T<:TimeType} = Vector(x) - Vector(y) From 4d39cf49fbab94af34fe4fe444e0b99ed5d193fb Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 27 Aug 2020 10:31:40 +0100 Subject: [PATCH 5/6] Hardcode `DatePeriod` and `TimePeriod` subtypes Avoids the usage of `subtypes` function, which would need to be imported from `InteractiveUtils` otherwise --- stdlib/Dates/test/arithmetic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index 6be02c2fa255a..7f9ffdc135194 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -500,7 +500,7 @@ end end @testset "Missing arithmetic" begin - for t ∈ [Date; Time; subtypes(DatePeriod); subtypes(TimePeriod)] + for t ∈ [Date Time Day Month Week Year Hour Microsecond Millisecond Minute Nanosecond Second] @test ismissing(t(1) + missing) @test ismissing(missing + t(1)) @test ismissing(t(1) - missing) From 8631987979ea83badc131cdef60bc35c42b4b417 Mon Sep 17 00:00:00 2001 From: Nils Date: Thu, 27 Aug 2020 11:34:08 +0100 Subject: [PATCH 6/6] Update stdlib/Dates/test/arithmetic.jl Co-authored-by: Milan Bouchet-Valat --- stdlib/Dates/test/arithmetic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index 7f9ffdc135194..a1a6884d4b0c3 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -500,7 +500,7 @@ end end @testset "Missing arithmetic" begin - for t ∈ [Date Time Day Month Week Year Hour Microsecond Millisecond Minute Nanosecond Second] + for t ∈ [Date, Time, Day, Month, Week, Year, Hour, Microsecond, Millisecond, Minute, Nanosecond, Second] @test ismissing(t(1) + missing) @test ismissing(missing + t(1)) @test ismissing(t(1) - missing)