#ifndef __JEEP__JDATE__ #define __JEEP__JDATE__ #include #include #include #include #include #include #include "JLang/JComparable.hh" #include "JLang/JWhiteSpacesFacet.hh" #include "Jeep/JPrint.hh" /** * \author mdejong */ namespace JEEP {} namespace JPP { using namespace JEEP; } namespace JEEP { using JLANG::JComparable; /** * Auxiliary class for simple date. * * The corresponding ASCII format is yyyy\mm\dd,\n * where * yyyy corresponds to the year, * mm to the month and * dd to the day.\n * The separator is defined by the template parameter. */ template struct JDate : public JComparable< JDate > { /** * Separation character. */ static const char SEPARATOR = JSeparator_t; /** * Default constructor. */ JDate() : year (0), month(0), day (0) {} /** * Constructor. * * \param year year * \param month month * \param day day */ JDate(const int year, const int month, const int day) : year (year), month(month), day (day) {} /** * Constructor. * * \param date date */ JDate(const std::string& date) { std::istringstream in(date); in >> *this; } static JDate min() { return JDate(std::numeric_limits::lowest(), 1, 1); } //!< Minimal date static JDate max() { return JDate(std::numeric_limits::max(), 12, 31); } //!< Maximal date /** * Less-than method. * * \param date date * \return true if this date earlier than given date; else false */ bool less(const JDate& date) const { if (this->year == date.year) { if (this->month == date.month) return this->day < date.day; else return this->month < date.month; } else { return this->year < date.year; } } /** * Read date from input stream. * * \param in input stream * \param object date * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JDate& object) { using namespace std; using namespace JPP; object = JDate(); while (isspace(in.peek())) { in.ignore(); } const locale loc = in.imbue(locale(in.getloc(), new JWhiteSpacesFacet(in.getloc(), JDate::SEPARATOR))); in >> object.year >> object.month >> object.day; in.imbue(loc); return in; } /** * Write date to output stream. * * \param out output stream * \param object date * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JDate& object) { return out << FILL(4,'0') << object.year << JDate::SEPARATOR << FILL(2,'0') << object.month << JDate::SEPARATOR << FILL(2,'0') << object.day << FILL(); } int year; //!< year int month; //!< month int day; //!< day }; } #endif