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
  • Loading branch information
JeffBezanson authored Nov 1, 2023
1 parent e2a6424 commit 405ce11
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 405ce11

Please sign in to comment.