Skip to content
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

MethodError trying to print a AnchoredInterval{Week(-1)} #163

Open
nickrobinson251 opened this issue Apr 9, 2021 · 1 comment
Open

MethodError trying to print a AnchoredInterval{Week(-1)} #163

nickrobinson251 opened this issue Apr 9, 2021 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@nickrobinson251
Copy link
Contributor

nickrobinson251 commented Apr 9, 2021

The behaviour for printing a AnchoredInterval{Day(-1)} is:

julia> dt = DateTime(2016, 8, 11, 2)
2016-08-11T02:00:00

julia> AnchoredInterval{Day(-1)}(dt)
AnchoredInterval{Day(-1), DateTime, Open, Closed}(DateTime("2016-08-11T02:00:00"))

julia> print(ans)
(DE 2016-08-11 02:00:00]

And this style of print is available for AnchoredInterval{T(-1)} if T is Day, Month, Year or Millisecond, Second, Minute, Hour

But errors for Week, Quarter (new in Julia v1.6), or Nanosecond, Microsecond

E.g. for AnchoredInterval{Week(-1)}

julia> dt = DateTime(2016, 8, 11, 2)
2016-08-11T02:00:00

julia> AnchoredInterval{Week(-1)}(dt)
AnchoredInterval{Week(-1), DateTime, Open, Closed}(DateTime("2016-08-11T02:00:00"))

julia> print(ans)
ERROR: MethodError: no method matching prefix(::Type{Week})
Closest candidates are:
  prefix(::Period) at /Users/nick/.julia/packages/Intervals/ua9cq/src/description.jl:58
  prefix(::Type{Year}) at /Users/nick/.julia/packages/Intervals/ua9cq/src/description.jl:62
  prefix(::Type{Month}) at /Users/nick/.julia/packages/Intervals/ua9cq/src/description.jl:63
  ...
Stacktrace:
 [1] prefix(p::Week)
   @ Intervals ~/.julia/packages/Intervals/ua9cq/src/description.jl:59
 [2] description(dt::DateTime, p::Week, suffix::String)
   @ Intervals ~/.julia/packages/Intervals/ua9cq/src/description.jl:41
 [3] description(interval::AnchoredInterval{Week(-1), DateTime, Open, Closed}, s::String)
   @ Intervals ~/.julia/packages/Intervals/ua9cq/src/description.jl:4
 [4] description(i::AnchoredInterval{Week(-1), DateTime, Open, Closed})
   @ Intervals ~/.julia/packages/Intervals/ua9cq/src/description.jl:1
 [5] print(io::Base.TTY, interval::AnchoredInterval{Week(-1), DateTime, Open, Closed})
   @ Intervals ~/.julia/packages/Intervals/ua9cq/src/anchoredinterval.jl:256
 [6] print(xs::AnchoredInterval{Week(-1), DateTime, Open, Closed})
   @ Base ./coreio.jl:3

Assuming there's no reason not to support these Periods, i believe this is just a bug in the print defintions.

I think it can be fixed by adding methods to prefix:

prefix(::Type{Year}) = "Y"
prefix(::Type{Month}) = "Mo"
prefix(::Type{Day}) = "D"
prefix(::Type{Hour}) = "H"
prefix(::Type{Minute}) = "M"
prefix(::Type{Second}) = "S"
prefix(::Type{Millisecond}) = "ms"

And adding appropriate tests, like

AnchoredInterval{Day(-1)}(DateTime(dt)),
"(DE 2016-08-11 02:00:00]",
string(
if VERSION >= v"1.6.0"
"AnchoredInterval{$(repr(Day(-1))), DateTime, Open, Closed}"
else
"AnchoredInterval{$(repr(Day(-1))),DateTime,Open,Closed}"
end,
"($(repr(DateTime(2016, 8, 11, 2))))",


originally reported by @Ianlmgoddard

@nickrobinson251 nickrobinson251 added bug Something isn't working good first issue Good for newcomers labels Apr 9, 2021
@omus
Copy link
Collaborator

omus commented Apr 12, 2021

Assuming there's no reason not to support these Periods, i believe this is just a bug in the print defintions.

There isn't a reason these aren't supported beyond a prefix letter needs to be chosen for these Period types. I do believe Microsecond and Nanosecond didn't exist when this code was originally written.

I'd also suggest defining prefix(T::Type{<:Period}) = error("A prefix for period $T has not yet been defined") or alternatively add a comment to prefix to make this behaviour clearer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants