From 70a9efc863f5531193ac6983735f48b9476753af Mon Sep 17 00:00:00 2001
From: Daniel Vedder <daniel.vedder@idiv.de>
Date: Thu, 1 Aug 2024 17:58:11 +0200
Subject: [PATCH] Added AnnualDate functionality

---
 src/Persefone.jl     |  5 +++++
 src/nature/macros.jl | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/src/Persefone.jl b/src/Persefone.jl
index ad32aef..093a63a 100644
--- a/src/Persefone.jl
+++ b/src/Persefone.jl
@@ -116,6 +116,11 @@ import Base./ # enable division with different length/area unit types
 /(x::S,y::T) where {S<:Area, T<:Area} = (upreferred(x)/m²) / (upreferred(y)/m²)
 /(x::S,y::T) where {S<:Mass, T<:Mass} = (upreferred(x)/g) / (upreferred(y)/g)
 
+## Utility type and function for working wth recurring dates
+const AnnualDate = Tuple{Int64,Int64}
+function thisyear(ad::AnnualDate, model::SimulationModel) = Date(year(model.date), ad...)
+function nextyear(ad::AnnualDate, model::SimulationModel) = Date(year(model.date)+Year(1), ad...)
+
 """
     SimulationModel
 
diff --git a/src/nature/macros.jl b/src/nature/macros.jl
index add747c..54e3bef 100644
--- a/src/nature/macros.jl
+++ b/src/nature/macros.jl
@@ -492,3 +492,22 @@ macro follow(leader, distance)
     :(followanimal!($(esc(:self)), $(esc(leader)), $(esc(:model)), $(esc(distance))))
 end
 
+"""
+    @thisyear(annualdate)
+
+Construct a date object referring to the current model year from an AnnualDate
+(== Tuple{Int64,Int64}).
+"""
+macro thisyear(annualdate)
+    :(thisyear($(esc(annualdate)), $(esc(:model))))
+end
+
+"""
+    @nextyear(annualdate)
+
+Construct a date object referring to the next year in the model from an AnnualDate
+(== Tuple{Int64,Int64}).
+"""
+macro nextyear(annualdate)
+    :(nextyear($(esc(annualdate)), $(esc(:model))))
+end
-- 
GitLab