- Moved an invariant to greater scope.
This commit is contained in:
Paul Beckingham 2014-05-24 11:37:21 -04:00
parent b532b94b81
commit c3a759c63d

View file

@ -124,13 +124,24 @@ static int daysInMonth (int year, int month)
// ascension = // ascension =
// pentecost = // pentecost =
// goodfriday = // goodfriday =
// midsommar = // midsommar = midnight, 1st Saturday after 20th June
// midsommarafton = // midsommarafton = midnight, 1st Friday after 19th June
// Nth = // Nth =
bool namedDates (const std::string& name, Variant& value) bool namedDates (const std::string& name, Variant& value)
{ {
time_t now = time (NULL); time_t now = time (NULL);
struct tm* t = localtime (&now);
/*
std::cout << "# now t\n"
<< "# tm_year=" << t->tm_year << "\n"
<< "# tm_mon=" << t->tm_mon << "\n"
<< "# tm_mday=" << t->tm_mday << "\n"
<< "# tm_hour=" << t->tm_hour << "\n"
<< "# tm_min=" << t->tm_min << "\n"
<< "# tm_sec=" << t->tm_sec << "\n"
<< "# tm_isdst=" << t->tm_isdst << "\n";
*/
int i; int i;
// TODO Extract helper functions from this code. // TODO Extract helper functions from this code.
@ -143,21 +154,18 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "today" || name == "sod") else if (name == "today" || name == "sod")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
value = Variant (mktime (t), Variant::type_date); value = Variant (mktime (t), Variant::type_date);
} }
else if (name == "yesterday") else if (name == "yesterday")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
value = Variant (mktime (t) - 86400, Variant::type_date); value = Variant (mktime (t) - 86400, Variant::type_date);
} }
else if (name == "tomorrow" || name == "eod") else if (name == "tomorrow" || name == "eod")
{ {
struct tm* t = localtime (&now);
t->tm_mday++; t->tm_mday++;
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
value = Variant (mktime (t), Variant::type_date); value = Variant (mktime (t), Variant::type_date);
@ -165,8 +173,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (isDay (name, i)) else if (isDay (name, i))
{ {
struct tm* t = localtime (&now);
if (t->tm_wday >= i) if (t->tm_wday >= i)
t->tm_mday += i - t->tm_wday + 7; t->tm_mday += i - t->tm_wday + 7;
else else
@ -178,7 +184,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (isMonth (name, i)) else if (isMonth (name, i))
{ {
struct tm* t = localtime (&now);
if (t->tm_mon >= i) if (t->tm_mon >= i)
t->tm_year++; t->tm_year++;
@ -190,7 +195,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "soy") else if (name == "soy")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mon = 0; t->tm_mon = 0;
t->tm_mday = 1; t->tm_mday = 1;
@ -199,7 +203,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "eoy") else if (name == "eoy")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mon = 11; t->tm_mon = 11;
t->tm_mday = 31; t->tm_mday = 31;
@ -208,7 +211,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "socm") else if (name == "socm")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mday = 1; t->tm_mday = 1;
value = Variant (mktime (t), Variant::type_date); value = Variant (mktime (t), Variant::type_date);
@ -216,7 +218,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "som") else if (name == "som")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mon++; t->tm_mon++;
@ -232,7 +233,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "eom" || name == "eocm") else if (name == "eom" || name == "eocm")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mday = daysInMonth (t->tm_year + 1900, t->tm_mon + 1); t->tm_mday = daysInMonth (t->tm_year + 1900, t->tm_mon + 1);
value = Variant (mktime (t), Variant::type_date); value = Variant (mktime (t), Variant::type_date);
@ -283,7 +283,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "soq" || name == "eoq") else if (name == "soq" || name == "eoq")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_mon += 3 - (t->tm_mon % 3); t->tm_mon += 3 - (t->tm_mon % 3);
@ -303,7 +302,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "later" || name == "someday") else if (name == "later" || name == "someday")
{ {
struct tm* t = localtime (&now);
t->tm_hour = t->tm_min = t->tm_sec = 0; t->tm_hour = t->tm_min = t->tm_sec = 0;
t->tm_year = 138; t->tm_year = 138;
t->tm_mon = 0; t->tm_mon = 0;
@ -317,8 +315,6 @@ bool namedDates (const std::string& name, Variant& value)
name == "pentecost" || name == "pentecost" ||
name == "goodfriday") name == "goodfriday")
{ {
struct tm* t = localtime (&now);
int Y = t->tm_year + 1900; int Y = t->tm_year + 1900;
int a = Y % 19; int a = Y % 19;
int b = Y / 100; int b = Y / 100;
@ -351,7 +347,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "midsommar") else if (name == "midsommar")
{ {
struct tm* t = localtime (&now);
t->tm_mon = 5; // June. t->tm_mon = 5; // June.
t->tm_mday = 20; // Saturday after 20th. t->tm_mday = 20; // Saturday after 20th.
t->tm_hour = t->tm_min = t->tm_sec = 0; // Midnight. t->tm_hour = t->tm_min = t->tm_sec = 0; // Midnight.
@ -366,7 +361,6 @@ bool namedDates (const std::string& name, Variant& value)
else if (name == "midsommarafton") else if (name == "midsommarafton")
{ {
struct tm* t = localtime (&now);
t->tm_mon = 5; // June. t->tm_mon = 5; // June.
t->tm_mday = 19; // Friday after 19th. t->tm_mday = 19; // Friday after 19th.
t->tm_hour = t->tm_min = t->tm_sec = 0; // Midnight. t->tm_hour = t->tm_min = t->tm_sec = 0; // Midnight.
@ -409,7 +403,6 @@ bool namedDates (const std::string& name, Variant& value)
ordinal == "rd" || ordinal == "rd" ||
ordinal == "th") ordinal == "th")
{ {
struct tm* t = localtime (&now);
int y = t->tm_year + 1900; int y = t->tm_year + 1900;
int m = t->tm_mon + 1; int m = t->tm_mon + 1;
int d = t->tm_mday; int d = t->tm_mday;