From 7175f4577ff773dac78356a85002671f9430afee Mon Sep 17 00:00:00 2001 From: Ben Baumgold <4933671+baumgold@users.noreply.github.com> Date: Tue, 9 May 2023 12:49:15 -0400 Subject: [PATCH] TimeType subtraction using promote --- stdlib/Dates/src/arithmetic.jl | 1 + stdlib/Dates/test/arithmetic.jl | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index 6537f4e1caa82..a847f749d0154 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -7,6 +7,7 @@ # TimeType arithmetic (+)(x::TimeType) = x (-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant +(-)(x::TimeType, y::TimeType) = -(promote(x, y)...) # Date-Time arithmetic """ diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index 485fea5624066..2e684815a3c86 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -10,6 +10,13 @@ using Dates b = Dates.Time(11, 59, 59) @test Dates.CompoundPeriod(a - b) == Dates.Hour(12) end + +@testset "TimeType arithmetic" begin + a = Date(2023, 5, 1) + b = DateTime(2023, 5, 2) + @test b - a == Day(1) +end + @testset "Wrapping arithmetic for Months" begin # This ends up being trickier than expected because # the user might do 2014-01-01 + Month(-14)