From 3a4b1401d359ff9c94641272d68680b1f411202b Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 1 Nov 2023 13:18:08 -0400 Subject: [PATCH] further fix to the new promoting method for AbstractDateTime subtraction (#51967) (cherry picked from commit 405ce111a86ea85a97734efed0ea995cfdc7f56c) --- stdlib/Dates/src/arithmetic.jl | 2 +- stdlib/Dates/test/arithmetic.jl | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/Dates/src/arithmetic.jl b/stdlib/Dates/src/arithmetic.jl index b379747c5f9e9..83a2873b43409 100644 --- a/stdlib/Dates/src/arithmetic.jl +++ b/stdlib/Dates/src/arithmetic.jl @@ -7,7 +7,7 @@ # TimeType arithmetic (+)(x::TimeType) = x (-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant -(-)(x::DateTime, y::DateTime) = x.instant - y.instant +(-)(x::T, y::T) where {T<:AbstractDateTime} = x.instant - y.instant (-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...) # Date-Time arithmetic diff --git a/stdlib/Dates/test/arithmetic.jl b/stdlib/Dates/test/arithmetic.jl index d266526496c45..e3ed6ace57c69 100644 --- a/stdlib/Dates/test/arithmetic.jl +++ b/stdlib/Dates/test/arithmetic.jl @@ -14,10 +14,15 @@ end struct MonthlyDate <: TimeType instant::Dates.UTInstant{Month} end +struct OtherTime <: Dates.AbstractDateTime + instant::Dates.UTInstant{Nanosecond} +end @testset "TimeType arithmetic" begin @test_throws MethodError DateTime(2023, 5, 2) - Date(2023, 5, 1) # check that - between two same-type TimeTypes works by default @test MonthlyDate(Dates.UTInstant(Month(10))) - MonthlyDate(Dates.UTInstant(Month(1))) == Month(9) + # ... and between two same-type AbstractDateTimes + @test OtherTime(Dates.UTInstant(Nanosecond(2))) - OtherTime(Dates.UTInstant(Nanosecond(1))) == Nanosecond(1) end @testset "Wrapping arithmetic for Months" begin