From 10450963cb238d2289c4626dc393b8655d622d91 Mon Sep 17 00:00:00 2001 From: Federico Hernandez Date: Tue, 23 Mar 2010 02:13:35 +0100 Subject: [PATCH] Eeaster algorithm --- src/Date.cpp | 26 ++++++++++++++++++++++++++ src/Date.h | 2 ++ src/tests/date.t.cpp | 20 +++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Date.cpp b/src/Date.cpp index 8a010f28d..4482bfc63 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -964,3 +964,29 @@ bool Date::isRelativeDate (const std::string& input) } //////////////////////////////////////////////////////////////////////////////// +time_t easter (int year) +{ + int Y = year; + int a = Y % 19; + int b = Y / 100; + int c = Y % 100; + int d = b / 4; + int e = b % 4; + int f = (b + 8) / 25; + int g = (b - f + 1) / 3; + int h = (19 * a + b - d - g + 15) % 30; + int i = c / 4; + int k = c % 4; + int L = (32 + 2 * e + 2 * i - h - k) % 7; + int m = (a + 11 * h + 22 * L) / 451; + int month = (h + L - 7 * m + 114) / 31; + int day = ((h + L - 7 * m + 114) % 31) + 1; + struct tm t = {0}; + t.tm_isdst = -1; // Requests that mktime determine summer time effect. + t.tm_mday = day; + t.tm_mon = month - 1; + t.tm_year = year - 1900; + return mktime (&t); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Date.h b/src/Date.h index b5c5f2822..72bb9f1c8 100644 --- a/src/Date.h +++ b/src/Date.h @@ -30,6 +30,8 @@ #include #include +time_t easter (int year); + class Date; class Date diff --git a/src/tests/date.t.cpp b/src/tests/date.t.cpp index 9898be661..671b3c567 100644 --- a/src/tests/date.t.cpp +++ b/src/tests/date.t.cpp @@ -34,7 +34,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (135); + UnitTest t (143); try { @@ -225,6 +225,24 @@ int main (int argc, char** argv) t.is (fromString13.minute (), 34, "ctor (std::string) -> N"); t.is (fromString13.second (), 56, "ctor (std::string) -> S"); + // Easter + Date e1 (easter(1980)); + t.is (e1.toString (), "4/6/1980", "Easter 4/6/1980"); + Date e2 (easter(1995)); + t.is (e2.toString (), "4/16/1995", "Easter 4/16/1995"); + Date e3 (easter(2000)); + t.is (e3.toString (), "4/23/2000", "Easter 4/23/2000"); + Date e4 (easter(2009)); + t.is (e4.toString (), "4/12/2009", "Easter 4/12/2009"); + Date e5 (easter(2010)); + t.is (e5.toString (), "4/4/2010", "Easter 4/4/2010"); + Date e6 (easter(2011)); + t.is (e6.toString (), "4/24/2011", "Easter 4/24/2011"); + Date e7 (easter(2012)); + t.is (e7.toString (), "4/8/2012", "Easter 4/8/2012"); + Date e8 (easter(2020)); + t.is (e8.toString (), "4/12/2020", "Easter 4/12/2020"); + // Relative dates. Date r1 ("today"); t.ok (r1.sameDay (now), "today = now");