New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support TimeSpan
construction from Dates.CompoundPeriod
, Dates.Period
#57
Conversation
CI is failing on 1.6 because the |
Okay, updated to fix failing 1.6 tests! |
src/TimeSpans.jl
Outdated
@@ -35,7 +36,7 @@ struct TimeSpan | |||
return new(start, stop) | |||
end | |||
function TimeSpan(start, stop) | |||
_to_ns = (t) -> isa(t, Dates.CompoundPeriod) ? sum(convert.(Nanosecond, t.periods)) : Nanosecond(t) | |||
_to_ns = (t) -> isa(t, Dates.CompoundPeriod) ? convert(Nanosecond, t) : Nanosecond(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should just always convert? That is the default behavior when you don't have an inner constructor, for example;
julia> struct A
n::Nanosecond
end
julia> A(1)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Nanosecond
Closest candidates are:
convert(::Type{T}, ::Dates.CompoundPeriod) where T<:Period at ~/.julia/juliaup/julia-1.8.5+0.x64.linux.gnu/share/julia/stdlib/v1.8/Dates/src/periods.jl:363
convert(::Type{Nanosecond}, ::Microsecond) at ~/.julia/juliaup/julia-1.8.5+0.x64.linux.gnu/share/julia/stdlib/v1.8/Dates/src/periods.jl:433
convert(::Type{Nanosecond}, ::Millisecond) at ~/.julia/juliaup/julia-1.8.5+0.x64.linux.gnu/share/julia/stdlib/v1.8/Dates/src/periods.jl:433
...
Stacktrace:
[1] A(n::Int64)
@ Main ./REPL[7]:2
[2] top-level scope
@ REPL[11]:1
You can see in the stacktrace that it tries to call convert
. But if you have an inner constructor, it calls that and doesn't have the default constructor w/ convert:
julia> struct B
n::Nanosecond
B(n::Nanosecond) = new(n)
end
julia> B(1)
ERROR: MethodError: no method matching B(::Int64)
Closest candidates are:
B(::Nanosecond) at REPL[9]:3
Stacktrace:
[1] top-level scope
@ REPL[10]:1
I believe we have the inner constructor to do the start == stop
case, but I don't think we were aiming to get rid of the convert constructors. So I think just auto-converting when we don't hit the ::Nanosecond
one might be the best approach:
_to_ns = (t) -> isa(t, Dates.CompoundPeriod) ? convert(Nanosecond, t) : Nanosecond(t) | |
_to_ns = convert(Nanosecond, t) |
Note that convert(Nanosecond, Nanosecond(1))
works, so even if start::CompoundPeriod
and stop::Nanosecond
, the extra convert should be harmless.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should! we weren't b/c I thought that Alex's "Compat" fix wasn't working, because I didn't implement it correctly :lol-cry:. BUT the most recent implementation does what you're suggesting, I think---take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh hang on! let me reread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah oops, yeah, agree w/ the current implementation!
I was suggesting something different (always convert
) but I missed that we already had
TimeSpan(start, stop) = TimeSpan(Nanosecond(start), Nanosecond(stop))
in the code base, in which case using the constructor (Nanosecond()
) instead of convert
makes sense as the fallback. Ok agreed with what we have!
No description provided.