#ifndef __JEEP__JTIME__ #define __JEEP__JTIME__ #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 time. * * The corresponding ASCII format is hh\mm\ss, * where * hh corresponds to the hour, * mm to the minutes and * ss to the seconds.\n * The separator is defined by the template parameter. */ template struct JTime : public JComparable< JTime > { /** * Separation character. */ static const char SEPARATOR = JSeparator_t; /** * Default constructor. */ JTime() : hour (0), minute(0), second(0) {} /** * Constructor. * * \param hour hour * \param minute minute * \param second second */ JTime(const int hour, const int minute, const int second) : hour (hour), minute(minute), second(second) {} /** * Constructor. * * \param time time */ JTime(const std::string& time) { std::istringstream in(time); in >> *this; } static JTime min() { return JTime(std::numeric_limits::lowest(), 1, 1); } //!< Minimal time static JTime max() { return JTime(std::numeric_limits::max(), 12, 31); } //!< Maximal time /** * Less-than method. * * \param time time * \return true if this time earlier than given time; else false */ bool less(const JTime& time) const { if (this->hour == time.hour) { if (this->minute == time.minute) return this->second < time.second; else return this->minute < time.minute; } else { return this->hour < time.hour; } } /** * Read time from input stream. * * \param in input stream * \param object time * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JTime& object) { using namespace std; using namespace JPP; object = JTime(); while (isspace(in.peek())) { in.ignore(); } const locale loc = in.imbue(locale(in.getloc(), new JWhiteSpacesFacet(in.getloc(), JTime::SEPARATOR))); in >> object.hour >> object.minute >> object.second; in.imbue(loc); return in; } /** * Write time to output stream. * * \param out output stream * \param object time * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JTime& object) { return out << FILL(2,'0') << object.hour << JTime::SEPARATOR << FILL(2,'0') << object.minute << JTime::SEPARATOR << FILL(2,'0') << object.second << FILL(); } int hour; //!< hour int minute; //!< minute int second; //!< second }; } #endif