Implemented comparison operators.

This commit is contained in:
Vincent Richard 2005-04-13 19:55:45 +00:00
parent b846a2efba
commit 6a913043d7
2 changed files with 170 additions and 33 deletions

View File

@ -83,32 +83,44 @@ void datetime::parse(const string& buffer, const string::size_type position,
while (p < pend && parserHelpers::isSpace(*p)) ++p; while (p < pend && parserHelpers::isSpace(*p)) ++p;
} }
while (p < pend && !parserHelpers::isDigit(*p)) ++p; bool dayParsed = false;
if (p < pend && parserHelpers::isDigit(*p)) if (parserHelpers::isAlpha(*p))
{ {
// Month day // Ill-formed date/time, this may be the month,
int day = 0; // so we skip day parsing (will be done later)
do
{
day = day * 10 + (*p - '0');
++p;
}
while (p < pend && parserHelpers::isDigit(*p));
m_day = (day >= 1 && day <= 31) ? day : 1;
while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p;
} }
else else
{ {
m_day = 1; while (p < pend && !parserHelpers::isDigit(*p)) ++p;
// Skip everything to the next field if (p < pend && parserHelpers::isDigit(*p))
while (p < pend && !parserHelpers::isSpace(*p)) ++p; {
while (p < pend && parserHelpers::isSpace(*p)) ++p; // Month day
int day = 0;
do
{
day = day * 10 + (*p - '0');
++p;
}
while (p < pend && parserHelpers::isDigit(*p));
m_day = (day >= 1 && day <= 31) ? day : 1;
while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
else
{
m_day = 1;
// Skip everything to the next field
while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
dayParsed = true;
} }
if (p < pend && parserHelpers::isAlpha(*p)) if (p < pend && parserHelpers::isAlpha(*p))
@ -215,29 +227,66 @@ void datetime::parse(const string& buffer, const string::size_type position,
{ {
m_month = JANUARY; m_month = JANUARY;
// Skip everything to the next field if (parserHelpers::isDigit(*p))
{
// Here, we expected a month, but it maybe
// a ill-formed date, so try to parse a year
// (we don't skip anything).
}
else
{
// Skip everything to the next field
while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
}
if (!dayParsed && p < pend && parserHelpers::isDigit(*p))
{
// Month day
int day = 0;
do
{
day = day * 10 + (*p - '0');
++p;
}
while (p < pend && parserHelpers::isDigit(*p));
m_day = (day >= 1 && day <= 31) ? day : 1;
while (p < pend && !parserHelpers::isSpace(*p)) ++p; while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p; while (p < pend && parserHelpers::isSpace(*p)) ++p;
} }
if (p < pend && parserHelpers::isDigit(*p)) if (p < pend && parserHelpers::isDigit(*p))
{ {
// Year // Check for ill-formed date/time and try to recover
int year = 0; if (p + 2 < pend && *(p + 2) == ':')
do
{ {
year = year * 10 + (*p - '0'); // Skip year (default to current), and advance
++p; // to time parsing
m_year = now().getYear();
} }
while (p < pend && parserHelpers::isDigit(*p)); else
{
// Year
int year = 0;
if (year < 70) m_year = year + 2000; do
else if (year < 1000) m_year = year + 1900; {
else m_year = year; year = year * 10 + (*p - '0');
++p;
}
while (p < pend && parserHelpers::isDigit(*p));
while (p < pend && !parserHelpers::isSpace(*p)) ++p; if (year < 70) m_year = year + 2000;
while (p < pend && parserHelpers::isSpace(*p)) ++p; else if (year < 1000) m_year = year + 1900;
else m_year = year;
while (p < pend && !parserHelpers::isSpace(*p)) ++p;
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
} }
else else
{ {
@ -711,4 +760,84 @@ void datetime::setSecond(const int second) { m_second = second; }
void datetime::setZone(const int zone) { m_zone = zone; } void datetime::setZone(const int zone) { m_zone = zone; }
const bool datetime::operator==(const datetime& other) const
{
return (m_year == other.m_year &&
m_month == other.m_month &&
m_day == other.m_day &&
m_hour == other.m_hour &&
m_minute == other.m_minute &&
m_second == other.m_second &&
m_zone == other.m_zone);
}
const bool datetime::operator!=(const datetime& other) const
{
return (m_year != other.m_year ||
m_month != other.m_month ||
m_day != other.m_day ||
m_hour != other.m_hour ||
m_minute != other.m_minute ||
m_second != other.m_second ||
m_zone != other.m_zone);
}
const bool datetime::operator<(const datetime& other) const
{
const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this);
const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other);
return (ut1.m_year < ut2.m_year &&
ut1.m_month < ut2.m_month &&
ut1.m_day < ut2.m_day &&
ut1.m_hour < ut2.m_hour &&
ut1.m_minute < ut2.m_minute &&
ut1.m_second < ut2.m_second);
}
const bool datetime::operator<=(const datetime& other) const
{
const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this);
const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other);
return (ut1.m_year <= ut2.m_year &&
ut1.m_month <= ut2.m_month &&
ut1.m_day <= ut2.m_day &&
ut1.m_hour <= ut2.m_hour &&
ut1.m_minute <= ut2.m_minute &&
ut1.m_second <= ut2.m_second);
}
const bool datetime::operator>(const datetime& other) const
{
const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this);
const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other);
return (ut1.m_year > ut2.m_year &&
ut1.m_month > ut2.m_month &&
ut1.m_day > ut2.m_day &&
ut1.m_hour > ut2.m_hour &&
ut1.m_minute > ut2.m_minute &&
ut1.m_second > ut2.m_second);
}
const bool datetime::operator>=(const datetime& other) const
{
const datetime ut1 = utility::datetimeUtils::localTimeToUniversalTime(*this);
const datetime ut2 = utility::datetimeUtils::localTimeToUniversalTime(other);
return (ut1.m_year >= ut2.m_year &&
ut1.m_month >= ut2.m_month &&
ut1.m_day >= ut2.m_day &&
ut1.m_hour >= ut2.m_hour &&
ut1.m_minute >= ut2.m_minute &&
ut1.m_second >= ut2.m_second);
}
} // vmime } // vmime

View File

@ -218,6 +218,14 @@ public:
datetime* clone() const; datetime* clone() const;
// Comparison
const bool operator==(const datetime& other) const;
const bool operator!=(const datetime& other) const;
const bool operator<(const datetime& other) const;
const bool operator<=(const datetime& other) const;
const bool operator>(const datetime& other) const;
const bool operator>=(const datetime& other) const;
// Current date and time // Current date and time
static const datetime now(); static const datetime now();