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..2af634bdf104c 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -196,6 +196,13 @@ struct CompoundPeriod <: AbstractTime end end +""" + Dates.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