diff --git a/src/Date.cpp b/src/Date.cpp index 07ccef926..df909dd7c 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include #include @@ -131,9 +133,13 @@ Date::Date ( const bool iso /* = true */, const bool epoch /* = true */) { - // Before parsing according to "format", perhaps this is a relative date? - if (isRelativeDate (input)) + // Check first to see if this is supported as a named date. + Variant v; + if (namedDates (input, v)) + { + _t = v.get_date (); return; + } // Parse a formatted date. Nibbler n (input); @@ -819,312 +825,3 @@ bool Date::isEpoch (const std::string& input) } //////////////////////////////////////////////////////////////////////////////// -// If the input string looks like a relative date, determine that date, set _t -// and return true. -// -// What is a relative date? All of the following should be recognizable, and -// converted to an absolute date: -// wednesday -// fri -// 23rd -// today -// tomorrow -// yesterday -// eow (end of week) -// eom (end of month) -// eoy (end of year) -// now -bool Date::isRelativeDate (const std::string& input) -{ - std::string in (lowerCase (input)); - Date today; - - std::vector supported; - for (unsigned int i = 0; i < NUM_RELATIVES; ++i) - supported.push_back (relatives[i]); - - // Hard-coded 3, despite rc.abbreviation.minimum. - std::vector matches; - if (autoComplete (in, supported, matches, 3) == 1) - { - std::string found = matches[0]; - - // If day name. - int dow; - if ((dow = Date::dayOfWeek (found)) != -1 || - found == "eow" || - found == "eoww" || - found == "eocw" || - found == "sow" || - found == "soww") - { - if (found == "eow" || found == "eoww") - dow = 5; - - if (found == "eocw") - dow = (Date::dayOfWeek (context.config.get ("weekstart")) + 6) % 7; - - if (found == "sow" || found == "soww") - dow = 1; - - if (today.dayOfWeek () >= dow) - today += (dow - today.dayOfWeek () + 7) * 86400; - else - today += (dow - today.dayOfWeek ()) * 86400; - - int m, d, y; - today.toMDY (m, d, y); - Date then (m, d, y); - - _t = then._t; - return true; - } - - else if (found == "socw") - { - // day S M T W T F S - // dow 0 1 2 3 4 5 6 - // ----------------------- - // weekstart ^ - // today1 ^ - // today2 ^ - // - // delta1 = 6 <-- (0 - 1 + 7) % 7 - // delta2 = 3 <-- (4 - 1 + 7) % 7 - - dow = Date::dayOfWeek (context.config.get ("weekstart")); - int delta = (today.dayOfWeek () - dow + 7) % 7; - today -= delta * 86400; - - Date then (today.month (), today.day (), today.year ()); - _t = then._t; - return true; - } - - else if (found == "today") - { - Date then (today.month (), - today.day (), - today.year ()); - _t = then._t; - return true; - } - else if (found == "tomorrow") - { - Date then (today.month (), - today.day (), - today.year ()); - _t = then._t + 86400; - return true; - } - else if (found == "yesterday") - { - Date then (today.month (), - today.day (), - today.year ()); - _t = then._t - 86400; - return true; - } - else if (found == "eom" || found == "eocm") - { - Date then (today.month (), - daysInMonth (today.month (), today.year ()), - today.year ()); - _t = then._t; - return true; - } - else if (found == "eoq") - { - int eoq_month = today.month () + 2 - (today.month () - 1) % 3; - Date then (eoq_month, - daysInMonth (eoq_month, today.year ()), - today.year ()); - _t = then._t; - return true; - } - else if (found == "eoy") - { - Date then (12, 31, today.year ()); - _t = then._t; - return true; - } - else if (found == "socm") - { - int m = today.month (); - int y = today.year (); - Date then (m, 1, y); - _t = then._t; - return true; - } - else if (found == "som") - { - int m = today.month () + 1; - int y = today.year (); - if (m > 12) - { - m -=12; - y++; - } - Date then (m, 1, y); - _t = then._t; - return true; - } - else if (found == "soq") - { - int m = today.month () + 3 - (today.month () - 1) % 3; - int y = today.year (); - if (m > 12) - { - m -=12; - y++; - } - Date then (m , 1, y); - _t = then._t; - return true; - } - else if (found == "soy") - { - Date then (1, 1, today.year () + 1); - _t = then._t; - return true; - } - else if (found == "goodfriday") - { - Date then (Date::easter(today.year())); - _t = then._t - 86400*2; - return true; - } - else if (found == "easter") - { - Date then (Date::easter(today.year())); - _t = then._t; - return true; - } - else if (found == "eastermonday") - { - Date then (Date::easter(today.year())); - _t = then._t + 86400; - return true; - } - else if (found == "ascension") - { - Date then (Date::easter(today.year())); - _t = then._t + 86400*39; - return true; - } - else if (found == "pentecost") - { - Date then (Date::easter(today.year())); - _t = then._t + 86400*49; - return true; - } - else if (found == "midsommar") - { - for (int midsommar = 20; midsommar <= 26; midsommar++) - { - Date then (6, midsommar, today.year ()); - if (6 == then.dayOfWeek ()) - { - _t = then._t; - return true; - } - } - } - else if (found == "midsommarafton") - { - for (int midsommar = 19; midsommar <= 25; midsommar++) - { - Date then (6, midsommar, today.year ()); - if (5 == then.dayOfWeek ()) - { - _t = then._t; - return true; - } - } - } - else if (found == "now") - { - _t = time (NULL); - return true; - } - else if (found == "later" || found == "someday") - { - Date then (1, 18, 2038); - _t = then._t; - return true; - } - } - - // Support "21st" to indicate the next date that is the 21st day. - else if (in.length () <= 4 && - isdigit (in[0])) - { - int number; - std::string ordinal; - - if (isdigit (in[1])) - { - number = atoi (in.substr (0, 2).c_str ()); - ordinal = lowerCase (in.substr (2)); - } - else - { - number = atoi (in.substr (0, 2).c_str ()); - ordinal = lowerCase (in.substr (1)); - } - - // Sanity check. - if (number <= 31) - { - if (ordinal == "st" || - ordinal == "nd" || - ordinal == "rd" || - ordinal == "th") - { - int m = today.month (); - int d = today.day (); - int y = today.year (); - - // If it is this month. - if (d < number && - number <= Date::daysInMonth (m, y)) - { - Date then (m, number, y); - _t = then._t; - return true; - } - - do - { - m++; - - if (m > 12) - { - m = 1; - y++; - } - } - while (number > Date::daysInMonth (m, y)); - - Date then (m, number, y); - _t = then._t; - return true; - } - } - } - - return false; -} - -//////////////////////////////////////////////////////////////////////////////// -const std::vector Date::get_relatives () -{ - std::vector all; - for (unsigned int i = 0; i < NUM_RELATIVES; ++i) - if (strcmp (relatives[i], "-")) - all.push_back (relatives[i]); - - return all; -} - -//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Date.h b/src/Date.h index 320139ab0..a1087469a 100644 --- a/src/Date.h +++ b/src/Date.h @@ -109,11 +109,8 @@ public: void operator++ (); // Prefix void operator++ (int); // Postfix - static const std::vector get_relatives (); - private: bool isEpoch (const std::string&); - bool isRelativeDate (const std::string&); protected: time_t _t;