- Removed ::isRelativeDate and instead used namedDates. Which is better.
This commit is contained in:
Paul Beckingham 2014-06-15 22:28:46 -04:00
parent 67d85b484b
commit ea2405330f
2 changed files with 8 additions and 314 deletions

View file

@ -34,6 +34,8 @@
#include <ctype.h> #include <ctype.h>
#include <Nibbler.h> #include <Nibbler.h>
#include <Date.h> #include <Date.h>
#include <Variant.h>
#include <Dates.h>
#include <text.h> #include <text.h>
#include <util.h> #include <util.h>
#include <i18n.h> #include <i18n.h>
@ -131,9 +133,13 @@ Date::Date (
const bool iso /* = true */, const bool iso /* = true */,
const bool epoch /* = true */) const bool epoch /* = true */)
{ {
// Before parsing according to "format", perhaps this is a relative date? // Check first to see if this is supported as a named date.
if (isRelativeDate (input)) Variant v;
if (namedDates (input, v))
{
_t = v.get_date ();
return; return;
}
// Parse a formatted date. // Parse a formatted date.
Nibbler n (input); 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 <std::string> supported;
for (unsigned int i = 0; i < NUM_RELATIVES; ++i)
supported.push_back (relatives[i]);
// Hard-coded 3, despite rc.abbreviation.minimum.
std::vector <std::string> 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 <std::string> Date::get_relatives ()
{
std::vector <std::string> all;
for (unsigned int i = 0; i < NUM_RELATIVES; ++i)
if (strcmp (relatives[i], "-"))
all.push_back (relatives[i]);
return all;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -109,11 +109,8 @@ public:
void operator++ (); // Prefix void operator++ (); // Prefix
void operator++ (int); // Postfix void operator++ (int); // Postfix
static const std::vector <std::string> get_relatives ();
private: private:
bool isEpoch (const std::string&); bool isEpoch (const std::string&);
bool isRelativeDate (const std::string&);
protected: protected:
time_t _t; time_t _t;