diff options
author | Vincent Richard <[email protected]> | 2005-04-13 19:55:45 +0000 |
---|---|---|
committer | Vincent Richard <[email protected]> | 2005-04-13 19:55:45 +0000 |
commit | 6a913043d7a814ed8cbda9f2b8816646314e77c2 (patch) | |
tree | df201aeec2ad0921f74fa665bd48c41dea831e2e /src/dateTime.cpp | |
parent | Fixed missing doc for param. (diff) | |
download | vmime-6a913043d7a814ed8cbda9f2b8816646314e77c2.tar.gz vmime-6a913043d7a814ed8cbda9f2b8816646314e77c2.zip |
Implemented comparison operators.
Diffstat (limited to 'src/dateTime.cpp')
-rw-r--r-- | src/dateTime.cpp | 187 |
1 files changed, 158 insertions, 29 deletions
diff --git a/src/dateTime.cpp b/src/dateTime.cpp index aba96e48..a2814a9c 100644 --- a/src/dateTime.cpp +++ b/src/dateTime.cpp @@ -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::isDigit(*p)) ++p; + bool dayParsed = false; - if (p < pend && parserHelpers::isDigit(*p)) + if (parserHelpers::isAlpha(*p)) { - // Month day - int day = 0; + // 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; - do + if (p < pend && parserHelpers::isDigit(*p)) { - day = day * 10 + (*p - '0'); - ++p; - } - while (p < pend && parserHelpers::isDigit(*p)); + // Month day + int day = 0; - m_day = (day >= 1 && day <= 31) ? day : 1; + do + { + day = day * 10 + (*p - '0'); + ++p; + } + while (p < pend && parserHelpers::isDigit(*p)); - while (p < pend && !parserHelpers::isSpace(*p)) ++p; - while (p < pend && parserHelpers::isSpace(*p)) ++p; - } - else - { - m_day = 1; + m_day = (day >= 1 && day <= 31) ? day : 1; - // Skip everything to the next field - 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; + } + 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)) @@ -215,30 +227,67 @@ void datetime::parse(const string& buffer, const string::size_type position, { m_month = JANUARY; - // Skip everything to the next field - while (p < pend && !parserHelpers::isSpace(*p)) ++p; - while (p < pend && parserHelpers::isSpace(*p)) ++p; + 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 (p < pend && parserHelpers::isDigit(*p)) + if (!dayParsed && p < pend && parserHelpers::isDigit(*p)) { - // Year - int year = 0; + // Month day + int day = 0; do { - year = year * 10 + (*p - '0'); + day = day * 10 + (*p - '0'); ++p; } while (p < pend && parserHelpers::isDigit(*p)); - if (year < 70) m_year = year + 2000; - else if (year < 1000) m_year = year + 1900; - else m_year = year; + 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; + + do + { + year = year * 10 + (*p - '0'); + ++p; + } + while (p < pend && parserHelpers::isDigit(*p)); + + if (year < 70) m_year = year + 2000; + 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 { 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 |