Skip to content

Commit

Permalink
further fix to the new promoting method for AbstractDateTime subtract…
Browse files Browse the repository at this point in the history
…ion (#51967)

(cherry picked from commit 405ce11)
  • Loading branch information
JeffBezanson authored and KristofferC committed Nov 6, 2023
1 parent 35264b7 commit 3a4b140
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions stdlib/Dates/test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a4b140

Please sign in to comment.