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
 top-level scope at In:1
 include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
 execute_code(::String, ::String) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:27
 execute_request(::ZMQ.Socket, ::IJulia.Msg) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\execute_request.jl:86
 #invokelatest#1 at .\essentials.jl:710 [inlined]
 invokelatest at .\essentials.jl:709 [inlined]
 eventloop(::ZMQ.Socket) at D:\Users\sambi\.julia\packages\IJulia\a1SNk\src\eventloop.jl: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.
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
Base.:-(::DateTime, ::Date) should return a
Period most likely in
Millisecond as is with
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.
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.
But what time do you want to use for the
Date? 00.00? Midday? Same time as the time in the other argument?
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.
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() - Date(2020)
julia> now() - DateTime(2020)
julia> today() - firstdayofyear(today())
julia> now() - firstdayofyear(now())
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.