Now() - Date(2020) errors

Hi All,

I am seeing the following behavior with Julia 1.5.2 with Win64.

julia> now() - Date(2020)
MethodError: no method matching -(::DateTime, ::Date)
Closest candidates are:
  -(::DateTime, !Matched::Month) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:60
  -(::DateTime, !Matched::Year) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:31
  -(::DateTime, !Matched::Period) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.5\Dates\src\arithmetic.jl:77
  ...

Stacktrace:
 [1] top-level scope at In[27]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
 [3] execute_code(::String, ::String) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:27
 [4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:86
 [5] #invokelatest#1 at .\essentials.jl:710 [inlined]
 [6] invokelatest at .\essentials.jl:709 [inlined]
 [7] eventloop(::ZMQ.Socket) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\eventloop.jl:8
 [8] (::IJulia.var"#15#18")() at .\task.jl:356

This is kind of non-intuitive. I feel DateTime and Date should be interchangeable where needed. Workaround is of course Date(now()) - Date(2020) or today() - Date(2020) or now() - DateTime(2020) but it may be just simpler if the system had a default behavior than erroring.

regards,

Sambit

Something like that would be implemented by a Base.:-(::Datetime, ::Date) method, but I suppose it doesn’t exist because you can’t generally decide if you want to return a Datetime or a Date.

Base.:-(::DateTime, ::Date) should return a Period most likely in Millisecond as is with Base:-(::DateTime, ::DateTime).

What you’re looking for is today() - Day(2020). I think the conversion should be made explicit because there is no “right choice to return”, different users would like different things, so prefering one over another is a debate. By making the conversion explicit, people would be clear which one they like.

3 Likes

Updated the description to reflect all possible options to choose. float() - int() returns default to float. So technically it’s not too hard to think of DateTime() - Date() can be mapped to Period in milliseconds.

Issue added: https://github.com/JuliaLang/julia/issues/38592

But what time do you want to use for the Date? 00.00? Midday? Same time as the time in the other argument?

1 Like

Base:-(dt::DateTime, d::Date) = dt - DateTime(d) Whatever time the constructor shall convert by default.

I think you meant to say now() - Year(2020), as I’m guessing Date(2020) is meant to convey 2020 years ago? But as you say, there is quite a bit of ambiguity here, and it seems inappropriate for a simple method like - to make these decisions implicitly.

1 Like

If I understand correctly Date(2020) refers to 1st Jan 2020, whereas Year(2020) refers to 2020 years. In this case it appears that OP wants the amount of time that has elapsed since the beginning of this year. That’s a reasonable question to ask, although I suppose now() - Year(2020) would lead to something similar.

julia> today()
2020-11-29

julia> now()
2020-11-29T01:52:24.232

julia> today() - Date(2020)
333 days

julia> now() - DateTime(2020)
28777954689 milliseconds

julia> today() - firstdayofyear(today())
333 days

julia> now() - firstdayofyear(now())
28777964880 milliseconds

Date and DateTime are type constructors respectively, where the year is required, but the other parameters are optional. The last two lines are perhaps more Julian. I think implicit conversion here is not the best idea, as the user should be clear which output they would want.

Year is a constructor for a time period.

6 Likes