Skip to content

Commit

Permalink
limit TimeType subtraction (#51743)
Browse files Browse the repository at this point in the history
Disallow some type combinations that don't make sense.

---------

Co-authored-by: Ben Baumgold <4933671+baumgold@users.noreply.github.com>
  • Loading branch information
JeffBezanson and baumgold committed Oct 18, 2023
1 parent 101e475 commit 8a889ff
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 4 additions & 2 deletions stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

# TimeType arithmetic
(+)(x::TimeType) = x
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
(-)(x::TimeType, y::TimeType) = -(promote(x, y)...)
(-)(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: 1 addition & 3 deletions stdlib/Dates/test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ using Dates
end

@testset "TimeType arithmetic" begin
a = Date(2023, 5, 1)
b = DateTime(2023, 5, 2)
@test b - a == Day(1)
@test_throws MethodError DateTime(2023, 5, 2) - Date(2023, 5, 1)
end

@testset "Wrapping arithmetic for Months" begin
Expand Down

0 comments on commit 8a889ff

Please sign in to comment.