[Dates] Division of Periods

Hi, I tried to use Dates module and divide Millseconds on Minutes and error happen.
Why operation didn’t dispatch to method " /(::P, ::P) where P<:Period" ?

Julia Version 1.6.5

julia> using Dates

julia> Dates.Millisecond(60_000) / Dates.Minute(1)
ERROR: MethodError: no method matching /(::Millisecond, ::Minute)
Closest candidates are:
  /(::StridedArray{P, N} where N, ::P) where P<:Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\deprecated.jl:44
  /(::AbstractRange{var"#s814"} where var"#s814"<:P, ::P) where P<:Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\ranges.jl:67
  /(::P, ::P) where P<:Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\periods.jl:82
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> Dates.Millisecond(60_000) isa Dates.Period
true

julia> Dates.Minute(1) isa Dates.Period
true
1 Like

For this dispatch to happened, the type of two objects need to be the same.

Try defining /(::P,::Q) where {P<:Period, Q<:Period) and it’ll work

1 Like

It’s this issue. On Julia 1.7.1 it was still giving the error above. But in the upcoming Julia 1.8, it has been addressed.

julia> versioninfo()
Julia Version 1.8.0-DEV.1483
Commit 76fa18281d (2022-02-09 22:23 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.3.0)
  CPU: Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.0 (ORCJIT, cyclone)
Environment:
  JULIA_EDITOR = Emacs

julia> using Dates

julia> Dates.Millisecond(60_000) / Dates.Minute(1)
1.0

Not sure what platform you are on, but building the master branch should be straightforward on Linux.

3 Likes

Ouch, I see, the method definition " /(::P, ::P) where P<:Period" means that both arguments must be not just subtypes of Period, but also have exactly the same type which is not the case.

P.S. Until Julia 1.8, I decided to use in my code “/(promote(x,y)…)” because ‘x’ and ‘y’ could come in different combinations of Hour, Minute, Second, Millisecond, etc… .

1 Like

Good to know it is addressed in 1.8! Until that I’m ok to use my bypass solution.

Exactly that’s the intended behavior. Looking at the GitHub issue also shows that Dates definitions are not really convertible to each other’s as easily as you’ll think, but if promoting them works, that’s the right way to go for now.