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

Add prefix methods for Week and Quarter Types. #164

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Intervals"
uuid = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
license = "MIT"
authors = ["Invenia Technical Computing"]
version = "1.5.0"
version = "1.5.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
9 changes: 9 additions & 0 deletions src/description.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ end

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

# `Quarter` defined after Julia1.6.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# `Quarter` defined after Julia1.6.
# `Quarter` defined in Julia v1.6.

if VERSION >= v"1.6"
prefix(::Type{Quarter}) = "Q"
end

# informative error if no prefix is defined for the given type.
prefix(T::Type{<:Period}) = error("A prefix for period $T has not yet been defined")
Comment on lines +75 to +77

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we'd like it to "just work" instead of throwing,

Suggested change
# informative error if no prefix is defined for the given type.
prefix(T::Type{<:Period}) = error("A prefix for period $T has not yet been defined")
# Full name if no prefix is defined.
prefix(T::Type{<:Period}) = string(T)

43 changes: 43 additions & 0 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,30 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
"($(repr(DateTime(2016, 9, 1))))",
),
),
(
AnchoredInterval{Week(-1)}(dt),
"(WE 2016-08-11 02:00:00]",
string(
if VERSION >= v"1.6.0"
"AnchoredInterval{$(repr(Week(-1))), DateTime, Open, Closed}"
else
"AnchoredInterval{$(repr(Week(-1))),DateTime,Open,Closed}"
end,
"($(repr(DateTime(2016, 8, 11, 2, 0, 0))))",
),
),
(
AnchoredInterval{Week(-1)}(ceil(dt, Week)),
"(WE 2016-08-15]",
string(
if VERSION >= v"1.6.0"
"AnchoredInterval{$(repr(Week(-1))), DateTime, Open, Closed}"
else
"AnchoredInterval{$(repr(Week(-1))),DateTime,Open,Closed}"
end,
"($(repr(DateTime(2016, 8, 15))))",
),
),
(
AnchoredInterval{Day(-1)}(DateTime(dt)),
"(DE 2016-08-11 02:00:00]",
Expand Down Expand Up @@ -516,6 +540,18 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
),
]

# add test for `Quarter`, only defined after Julia1.6
if VERSION >= v"1.6"
entry = (AnchoredInterval{Quarter(-1)}(ceil(Date(dt), Quarter)),
"(QE 2016-10-01]",
string(
"AnchoredInterval{$(repr(Quarter(-1))), Date, Open, Closed}",
"($(repr(Date(2016, 10, 01))))",
),
)
push!(tests, entry)
end

for (interval, printed, shown) in tests
@test sprint(print, interval) == printed
@test string(interval) == printed
Expand Down Expand Up @@ -549,6 +585,13 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
"AnchoredInterval{25,Char,Closed,Open}('a')"
end
@test sprint(show, interval) == shown

# Error if no prefix defined for the given period type
interval = AnchoredInterval{Microsecond(-1)}(Date(dt))
@test_throws ErrorException sprint(print, interval)

interval = AnchoredInterval{Nanosecond(-1)}(Date(dt))
@test_throws ErrorException sprint(print, interval)
end

@testset "equality" begin
Expand Down