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,6 +83,15 @@ void datetime::parse(const string& buffer, const string::size_type position,
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
bool dayParsed = false;
if (parserHelpers::isAlpha(*p))
{
// Ill-formed date/time, this may be the month,
// so we skip day parsing (will be done later)
}
else
{
while (p < pend && !parserHelpers::isDigit(*p)) ++p;
if (p < pend && parserHelpers::isDigit(*p))
@ -111,6 +120,9 @@ void datetime::parse(const string& buffer, const string::size_type position,
while (p < pend && parserHelpers::isSpace(*p)) ++p;
}
dayParsed = true;
}
if (p < pend && parserHelpers::isAlpha(*p))
{
// Month
@ -215,12 +227,48 @@ void datetime::parse(const string& buffer, const string::size_type position,
{
m_month = JANUARY;
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;
}
if (p < pend && parserHelpers::isDigit(*p))
{
// Check for ill-formed date/time and try to recover
if (p + 2 < pend && *(p + 2) == ':')
{
// Skip year (default to current), and advance
// to time parsing
m_year = now().getYear();
}
else
{
// Year
int year = 0;
@ -239,6 +287,7 @@ 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;
}
}
else
{
m_year = 1970;
@ -711,4 +760,84 @@ void datetime::setSecond(const int second) { m_second = second; }
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

View File

@ -218,6 +218,14 @@ public:
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
static const datetime now();