Skip to content

Commit ea14d2d

Browse files
committed
limit TimeType subtraction. replaces #49805
1 parent aba4c33 commit ea14d2d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

stdlib/Dates/src/arithmetic.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
# TimeType arithmetic
88
(+)(x::TimeType) = x
9-
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
10-
(-)(x::T, y::T) where {T<:AbstractDateTime} = x.instant - y.instant
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
1112
(-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...)
1213

1314
# Date-Time arithmetic

stdlib/Dates/test/arithmetic.jl

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ using Dates
1111
@test Dates.CompoundPeriod(a - b) == Dates.Hour(12)
1212
end
1313

14+
@testset "TimeType arithmetic" begin
15+
@test_throws MethodError DateTime(2023, 5, 2) - Date(2023, 5, 1)
16+
end
17+
1418
@testset "Wrapping arithmetic for Months" begin
1519
# This ends up being trickier than expected because
1620
# the user might do 2014-01-01 + Month(-14)

0 commit comments

Comments
 (0)