Made easter algorithm static in Date

This commit is contained in:
Federico Hernandez 2010-03-23 02:51:31 +01:00
parent 10450963cb
commit d3628c04db
3 changed files with 35 additions and 35 deletions

View file

@ -643,6 +643,32 @@ int Date::monthOfYear (const std::string& input)
return -1;
}
////////////////////////////////////////////////////////////////////////////////
time_t Date::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);
}
////////////////////////////////////////////////////////////////////////////////
int Date::month () const
{
@ -964,29 +990,3 @@ 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);
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -30,7 +30,6 @@
#include <stdio.h>
#include <string>
time_t easter (int year);
class Date;
@ -55,6 +54,7 @@ public:
static bool valid (const int, const int, const int, const int, const int, const int);
static bool valid (const int, const int, const int);
static time_t easter (int year);
static bool leapYear (int);
static int daysInMonth (int, int);
static std::string monthName (int);

View file

@ -226,21 +226,21 @@ int main (int argc, char** argv)
t.is (fromString13.second (), 56, "ctor (std::string) -> S");
// Easter
Date e1 (easter(1980));
Date e1 (Date::easter(1980));
t.is (e1.toString (), "4/6/1980", "Easter 4/6/1980");
Date e2 (easter(1995));
Date e2 (Date::easter(1995));
t.is (e2.toString (), "4/16/1995", "Easter 4/16/1995");
Date e3 (easter(2000));
Date e3 (Date::easter(2000));
t.is (e3.toString (), "4/23/2000", "Easter 4/23/2000");
Date e4 (easter(2009));
Date e4 (Date::easter(2009));
t.is (e4.toString (), "4/12/2009", "Easter 4/12/2009");
Date e5 (easter(2010));
Date e5 (Date::easter(2010));
t.is (e5.toString (), "4/4/2010", "Easter 4/4/2010");
Date e6 (easter(2011));
Date e6 (Date::easter(2011));
t.is (e6.toString (), "4/24/2011", "Easter 4/24/2011");
Date e7 (easter(2012));
Date e7 (Date::easter(2012));
t.is (e7.toString (), "4/8/2012", "Easter 4/8/2012");
Date e8 (easter(2020));
Date e8 (Date::easter(2020));
t.is (e8.toString (), "4/12/2020", "Easter 4/12/2020");
// Relative dates.