Skip to content

Commit

Permalink
promotion rules for solar year and month
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Mar 5, 2024
1 parent 815dedc commit c83a952
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/period.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@inline _hasyear0(::Type{T}) where T <: AbstractCFDateTime = true



"""
monthlength = daysinmonth(::Type{DT},y,m)
Expand Down
5 changes: 4 additions & 1 deletion test/test_resolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/test_year0.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c83a952

Please sign in to comment.