Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

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

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 @@ -11,8 +11,13 @@ using Dates
@test Dates.CompoundPeriod(a - b) == Dates.Hour(12)
end

struct MonthlyDate <: TimeType
instant::Dates.UTInstant{Month}
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)
end

@testset "Wrapping arithmetic for Months" begin
Expand Down