From 6535c23b27f218f13d0a41490af44da7b110d38a Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sat, 9 Jan 2021 22:22:43 -0500 Subject: [PATCH 1/3] Add the public `Dates.periods` function for getting the `Vector` of `Period`s that comprise a `CompoundPeriod` --- NEWS.md | 1 + stdlib/Dates/docs/src/index.md | 1 + stdlib/Dates/src/periods.jl | 7 +++++++ stdlib/Dates/test/periods.jl | 1 + 4 files changed, 10 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7793ba31b7f24..9f7bcf26866a2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -68,6 +68,7 @@ Standard library changes #### Dates +* The `Dates.periods` function can be used to get the `Vector` of `Period`s that comprise a `CompoundPeriod` ([#39169]). #### Statistics diff --git a/stdlib/Dates/docs/src/index.md b/stdlib/Dates/docs/src/index.md index 975c08711fc19..9f48b70b8fb5c 100644 --- a/stdlib/Dates/docs/src/index.md +++ b/stdlib/Dates/docs/src/index.md @@ -747,6 +747,7 @@ Dates.Period(::Any) Dates.CompoundPeriod(::Vector{<:Dates.Period}) Dates.value Dates.default +Dates.periods ``` ### Rounding Functions diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index 8d81262af19ec..ac59738c560de 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -196,6 +196,13 @@ struct CompoundPeriod <: AbstractTime end end +""" + periods(::CompoundPeriod) -> Vector{Period} + +Return the `Vector` of `Period`s that comprise the given `CompoundPeriod`. +""" +periods(x::CompoundPeriod) = x.periods + """ CompoundPeriod(periods) -> CompoundPeriod diff --git a/stdlib/Dates/test/periods.jl b/stdlib/Dates/test/periods.jl index f4dd65d0efd2a..81aacd1a9e54b 100644 --- a/stdlib/Dates/test/periods.jl +++ b/stdlib/Dates/test/periods.jl @@ -366,6 +366,7 @@ end @test isequal(d - h, 2d - 2h - 1d + 1h) @test sprint(show, y + m) == string(y + m) @test convert(Dates.CompoundPeriod, y) + m == y + m + @test Dates.periods(convert(Dates.CompoundPeriod, y)) == convert(Dates.CompoundPeriod, y).periods end @testset "compound period simplification" begin # reduce compound periods into the most basic form From 523cb35365874e1d8fadc43abb762da1d8598e33 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sun, 10 Jan 2021 21:18:11 -0500 Subject: [PATCH 2/3] Apply suggestions from code review --- stdlib/Dates/src/periods.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index ac59738c560de..0745ab0352e53 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -197,7 +197,7 @@ struct CompoundPeriod <: AbstractTime end """ - periods(::CompoundPeriod) -> Vector{Period} + Dates.periods(::CompoundPeriod) -> Vector{Period} Return the `Vector` of `Period`s that comprise the given `CompoundPeriod`. """ From dd8b43e0e94d7289b7c1937379a0121c4d15f2c6 Mon Sep 17 00:00:00 2001 From: Dilum Aluthge Date: Sun, 10 Jan 2021 21:35:24 -0500 Subject: [PATCH 3/3] Fix whitespace --- stdlib/Dates/src/periods.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index 0745ab0352e53..2af634bdf104c 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -199,7 +199,7 @@ end """ Dates.periods(::CompoundPeriod) -> Vector{Period} -Return the `Vector` of `Period`s that comprise the given `CompoundPeriod`. +Return the `Vector` of `Period`s that comprise the given `CompoundPeriod`. """ periods(x::CompoundPeriod) = x.periods