From c83a952efd51cde4f43107579959cf37cefea55b Mon Sep 17 00:00:00 2001 From: Alexander Barth Date: Tue, 5 Mar 2024 13:10:10 +0100 Subject: [PATCH] promotion rules for solar year and month --- src/period.jl | 10 ++++++++++ src/query.jl | 1 + test/test_resolution.jl | 5 ++++- test/test_year0.jl | 10 ++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/period.jl b/src/period.jl index 19a23a3..32413c2 100644 --- a/src/period.jl +++ b/src/period.jl @@ -96,6 +96,16 @@ function promote_rule(::Type{Period{T1,Tfactor1,Texponent1}}, end end +for Tfactor1 in (SOLAR_YEAR, SOLAR_YEAR ÷ 12) + @eval function promote_rule(::Type{Period{T1,Val($Tfactor1), Val(-3)}}, + ::Type{Period{T2,Tfactor2,Texponent2}}) where + {T1,T2,Tfactor2,Texponent2} + return promote_rule( + Period{T1,Val(1), Val(-3)}, + Period{T2,Tfactor2,Texponent2}) + end +end + function convert(::Type{Period{T1,Tfactor1,Texponent1}}, p::Period{T2,Tfactor2,Texponent2}) where diff --git a/src/query.jl b/src/query.jl index 68fa3fd..72abb7f 100644 --- a/src/query.jl +++ b/src/query.jl @@ -6,6 +6,7 @@ @inline _hasyear0(::Type{T}) where T <: AbstractCFDateTime = true + """ monthlength = daysinmonth(::Type{DT},y,m) diff --git a/test/test_resolution.jl b/test/test_resolution.jl index d6a83fe..62c6697 100644 --- a/test/test_resolution.jl +++ b/test/test_resolution.jl @@ -328,5 +328,8 @@ end @test CFTime.datetuple(CFTime.timedecode(0,"days since -4713-01-01T12:00:00","julian", prefer_datetime = false)) == (-4713, 1, 1, 12, 0, 0, 0) +dt = CFTime.timedecode([0,1],"years since 2000-01-01T00:00:00", prefer_datetime = false) +@test Dates.value(Dates.Millisecond(dt[2] - dt[1])) == CFTime.SOLAR_YEAR - +dt = CFTime.timedecode([0,1],"months since 2000-01-01T00:00:00", prefer_datetime = false) +@test Dates.value(Dates.Millisecond(dt[2] - dt[1])) == CFTime.SOLAR_YEAR ÷ 12 diff --git a/test/test_year0.jl b/test/test_year0.jl index 6bf546e..486f819 100644 --- a/test/test_year0.jl +++ b/test/test_year0.jl @@ -33,3 +33,13 @@ dt = CFTime.timedecode(-1,"days since 0001-01-01",prefer_datetime = false) @test dt == DateTimeStandard(-1,12,31) @test CFTime.year(dt) == -1 @test CFTime.month(dt) == 12 + +# test a dummy calendar with no year 0 and regular month length +struct DummyDataTime{T,Torigintuple} <: CFTime.AbstractCFDateTime{T,Torigintuple} +end +import CFTime: _cum_month_length, _hasyear0 +_cum_month_length(::Type{DummyDataTime}) = (0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360) +_hasyear0(::Type{DummyDataTime}) = false + +Z = datenum(DummyDataTime, -1, 1, 1) +@test datetuple_ymd(DummyDataTime,Z) == (-1,1,1)