From 04125b38af572cf1671c5fa4ea4b8d8af360d00d Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Thu, 19 Sep 2024 20:43:31 +1200 Subject: [PATCH] Add test and note for Dates.days(::Year) The average number of days in a year should be the same in both Unitful and the Dates stdlib. We also add a reference to this function in the documentation for this unit. --- docs/src/dates.md | 2 +- test/dates.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/src/dates.md b/docs/src/dates.md index 9fa0238a..521dc9e0 100644 --- a/docs/src/dates.md +++ b/docs/src/dates.md @@ -12,7 +12,7 @@ end The `Dates.FixedPeriod` union type includes all `Dates.Period`s that represent a fixed period of time, i.e., `Dates.Week`, `Dates.Day`, `Dates.Hour`, `Dates.Minute`, `Dates.Second`, `Dates.Millisecond`, `Dates.Microsecond`, and `Dates.Nanosecond`. These types can be converted to `Quantity`s or used in place of them. !!! note - `Dates.Year` does not represent a fixed period and cannot be converted to a `Quantity`. While Unitful's `yr` unit is exactly equal to 365.2425 days, a `Dates.Year` may contain 365 or 366 days (see [`Dates.isleapyear`](@ref)). + `Dates.Year` does not represent a fixed period and cannot be converted to a `Quantity`. While Unitful's `yr` unit is exactly equal to 365.2425 days, a `Dates.Year` may contain 365 or 366 days (see [`Dates.days(::Year)`](@ref) and [`Dates.isleapyear`](@ref)). Each `FixedPeriod` is considered equivalent to a `Quantity`. For example, `Dates.Millisecond(5)` corresponds to the quantity `Int64(5)*u"ms"`. A `FixedPeriod` can be converted to the equivalent `Quantity` with a constructor: diff --git a/test/dates.jl b/test/dates.jl index 25145b19..9a1ef3b5 100644 --- a/test/dates.jl +++ b/test/dates.jl @@ -206,6 +206,7 @@ @test u"μs"(CompoundPeriod(Day(365),Hour(5),Minute(49),Second(12))) === Rational{Int64}(31_556_952_000_000,1)u"μs" @test uconvert(u"ns", CompoundPeriod(Day(365),Hour(5),Minute(49),Second(12))) === Int64(31_556_952_000_000_000)u"ns" @test u"ns"(CompoundPeriod(Day(365),Hour(5),Minute(49),Second(12))) === Int64(31_556_952_000_000_000)u"ns" + @test uconvert(u"ms", 1u"yr") == (Dates.toms(Year(1)))u"ms" # Compare to Dates stdlib value of a year @test uconvert(u"ps", CompoundPeriod(Week(1),Hour(-1))) === Int64(601_200_000_000_000_000)u"ps" @test u"ps"(CompoundPeriod(Week(1),Hour(-1))) === Int64(601_200_000_000_000_000)u"ps" @test_throws DimensionError uconvert(u"m", CompoundPeriod(Day(365),Hour(5),Minute(49),Second(12)))