diff --git a/src/ISO8601.cpp b/src/ISO8601.cpp index 26c613073..b1295dcd6 100644 --- a/src/ISO8601.cpp +++ b/src/ISO8601.cpp @@ -25,6 +25,8 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include +#include #include #include #include @@ -513,6 +515,60 @@ ISO8601p::~ISO8601p () { } +//////////////////////////////////////////////////////////////////////////////// +ISO8601p::ISO8601p (const std::string& input) +{ + if (Lexer::isAllDigits (input)) + { + time_t value = (time_t) strtol (input.c_str (), NULL, 10); + if (value == 0 || value > 60) + { + _value = value; + return; + } + } + + std::string::size_type idx = 0; + parse (input, idx); +} + +//////////////////////////////////////////////////////////////////////////////// +ISO8601p& ISO8601p::operator= (const ISO8601p& other) +{ + if (this != &other) + { + _year = other._year; + _month = other._month; + _day = other._day; + _hours = other._hours; + _minutes = other._minutes; + _seconds = other._seconds; + _value = other._value; + } + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +bool ISO8601p::operator< (const ISO8601p& other) +{ + return _value < other._value; +} + +//////////////////////////////////////////////////////////////////////////////// +bool ISO8601p::operator> (const ISO8601p& other) +{ + return _value > other._value; +} + +//////////////////////////////////////////////////////////////////////////////// +ISO8601p::operator std::string () const +{ + std::stringstream s; + s << _value; + return s.str (); +} + //////////////////////////////////////////////////////////////////////////////// ISO8601p::operator time_t () const { @@ -576,6 +632,41 @@ void ISO8601p::clear () _value = 0; } +//////////////////////////////////////////////////////////////////////////////// +const std::string ISO8601p::format () const +{ + if (_value) + { + time_t t = _value; + int seconds = t % 60; t /= 60; + int minutes = t % 60; t /= 60; + int hours = t % 24; t /= 24; + int days = t % 30; t /= 30; + int months = t % 12; t /= 12; + int years = t; + + std::stringstream s; + s << 'P'; + if (years) s << years << 'Y'; + if (months) s << months << 'M'; + if (days) s << days << 'D'; + + if (hours || minutes || seconds) + { + s << 'T'; + if (hours) s << hours << 'H'; + if (minutes) s << minutes << 'M'; + if (seconds) s << seconds << 'S'; + } + + return s.str (); + } + else + { + return "PT0S"; + } +} + //////////////////////////////////////////////////////////////////////////////// // 'P' [nn 'Y'] [nn 'M'] [nn 'D'] ['T' [nn 'H'] [nn 'M'] [nn 'S']] bool ISO8601p::parse_designated (Nibbler& n) diff --git a/src/ISO8601.h b/src/ISO8601.h index fd1d87e63..a8fb5d915 100644 --- a/src/ISO8601.h +++ b/src/ISO8601.h @@ -72,11 +72,16 @@ class ISO8601p public: ISO8601p (); ~ISO8601p (); - ISO8601p (const ISO8601p&); // Unimplemented - ISO8601p& operator= (const ISO8601p&); // Unimplemented + ISO8601p (const ISO8601p&); // Unimplemented + ISO8601p (const std::string&); + ISO8601p& operator= (const ISO8601p&); + bool operator< (const ISO8601p&); + bool operator> (const ISO8601p&); + operator std::string () const; operator time_t () const; bool parse (const std::string&, std::string::size_type&); void clear (); + const std::string format () const; private: bool parse_designated (Nibbler&);