Skip to content

Commit

Permalink
limit TimeType subtraction. replaces #49805
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Oct 17, 2023
1 parent aba4c33 commit ea14d2d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

# TimeType arithmetic
(+)(x::TimeType) = x
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
(-)(x::T, y::T) where {T<:AbstractDateTime} = x.instant - y.instant
(-)(x::Date, y::Date) = x.instant - y.instant
(-)(x::Time, y::Time) = x.instant - y.instant
(-)(x::DateTime, y::DateTime) = x.instant - y.instant
(-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...)

# Date-Time arithmetic
Expand Down
4 changes: 4 additions & 0 deletions stdlib/Dates/test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ using Dates
@test Dates.CompoundPeriod(a - b) == Dates.Hour(12)
end

@testset "TimeType arithmetic" begin
@test_throws MethodError DateTime(2023, 5, 2) - Date(2023, 5, 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)
Expand Down

0 comments on commit ea14d2d

Please sign in to comment.