Why does Dates allow non-broadcasted arithmatic?

https://github.com/JuliaLang/julia/blob/f8d82ccba81223c9d0d43e4077acb2a208bedc7a/stdlib/Dates/src/arithmetic.jl#L94

In the code for Dates there is the following:

# TimeType, AbstractArray{TimeType}
(-)(x::AbstractArray{T}, y::T) where {T<:TimeType} = x .- y
(-)(y::T, x::AbstractArray{T}) where {T<:TimeType} = y .- x

Is this intended? Was it some code to get around deprecation warnings for a period of time?

2 Likes

I imagine this is just an oversight. It should be possible to deprecate it given that Dates is in stdlib. At least it’s worth filing an issue.

2 Likes

For posterity, the issue is #28672.

Cheers,
Kevin

1 Like

This behavior continues. Reading the issue in the last reply and following links from there to other discussions, date arithmetic yet continues to show ‘auto-broadcasting.’ Upon encountering it last year, as a relative Julia newbie, I was puzzled.

julia> versioninfo()
Julia Version 1.7.0-rc3
Commit 3348de4ea6 (2021-11-15 08:22 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.1.0)
  CPU: Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)
Environment:
  JULIA_EDITOR = Emacs

# dataframe, df, with column named dtime
df.dtime = df.dtime + Dates.Year(1900)
## why don't datetimes need broadcasting syntax?
## 3 + [1, 2, 3]  ## error
## 3 .+ [1, 2, 3] ## works

julia> foo
20-element Vector{DateTime}:
 1999-09-01T08:21:00
 1999-08-29T12:28:00
 1999-06-30T09:10:00
 1999-08-08T15:29:00
 1999-07-02T15:17:00
 1999-09-01T08:25:00
 1999-08-29T12:31:00
 1999-06-30T10:56:00
 1999-07-01T10:39:00
 1999-05-25T13:03:00
 1999-05-11T17:11:00
 1999-08-21T02:19:00
 1999-09-01T07:58:00
 1999-08-30T03:51:00
 1997-02-28T17:14:00
 1996-10-01T08:34:00
 1996-09-27T09:13:00
 1996-09-27T12:50:00
 1996-09-27T21:45:00
 1996-09-28T20:20:00

julia> foo + Dates.Year(1)
20-element Vector{DateTime}:
 2000-09-01T08:21:00
 2000-08-29T12:28:00
 2000-06-30T09:10:00
 2000-08-08T15:29:00
 2000-07-02T15:17:00
 2000-09-01T08:25:00
 2000-08-29T12:31:00
 2000-06-30T10:56:00
 2000-07-01T10:39:00
 2000-05-25T13:03:00
 2000-05-11T17:11:00
 2000-08-21T02:19:00
 2000-09-01T07:58:00
 2000-08-30T03:51:00
 1998-02-28T17:14:00
 1997-10-01T08:34:00
 1997-09-27T09:13:00
 1997-09-27T12:50:00
 1997-09-27T21:45:00
 1997-09-28T20:20:00

julia> foo - Dates.Year(1)
20-element Vector{DateTime}:
 1998-09-01T08:21:00
 1998-08-29T12:28:00
 1998-06-30T09:10:00
 1998-08-08T15:29:00
 1998-07-02T15:17:00
 1998-09-01T08:25:00
 1998-08-29T12:31:00
 1998-06-30T10:56:00
 1998-07-01T10:39:00
 1998-05-25T13:03:00
 1998-05-11T17:11:00
 1998-08-21T02:19:00
 1998-09-01T07:58:00
 1998-08-30T03:51:00
 1996-02-28T17:14:00
 1995-10-01T08:34:00
 1995-09-27T09:13:00
 1995-09-27T12:50:00
 1995-09-27T21:45:00
 1995-09-28T20:20:00

https://github.com/JuliaLang/julia/commit/b8e3b91716c26613c9fc856eb8fa73e77a388e18#diff-af0c0e01cab8cc433de9e334f50c707bef65307401216b6d919ec93c96a3436b

Yes, but see last comment here and this.

well yeah we missed it at 1.0 (i.e. 0.7) so there’s nothing we can do now. It’s already been noticed and thus moved to deprecated.jl

1 Like