Skip to content

Commit 8a889ff

Browse files
limit TimeType subtraction (#51743)
Disallow some type combinations that don't make sense. --------- Co-authored-by: Ben Baumgold <[email protected]>
1 parent 101e475 commit 8a889ff

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

stdlib/Dates/src/arithmetic.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
# TimeType arithmetic
88
(+)(x::TimeType) = x
9-
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
10-
(-)(x::TimeType, y::TimeType) = -(promote(x, y)...)
9+
(-)(x::Date, y::Date) = x.instant - y.instant
10+
(-)(x::Time, y::Time) = x.instant - y.instant
11+
(-)(x::DateTime, y::DateTime) = x.instant - y.instant
12+
(-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...)
1113

1214
# Date-Time arithmetic
1315
"""

stdlib/Dates/test/arithmetic.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ using Dates
1212
end
1313

1414
@testset "TimeType arithmetic" begin
15-
a = Date(2023, 5, 1)
16-
b = DateTime(2023, 5, 2)
17-
@test b - a == Day(1)
15+
@test_throws MethodError DateTime(2023, 5, 2) - Date(2023, 5, 1)
1816
end
1917

2018
@testset "Wrapping arithmetic for Months" begin

0 commit comments

Comments
 (0)