#ifndef TIME_H #define TIME_H #include #include #include /** * An exception that is thrown by the Time class for invalid formats */ class __BadTimeException { public: const std::string time; /** The string that failed to be parsed */ /** * __BadTimeException class constructor * @param s The string that failed to be parsed */ __BadTimeException(const std::string& s); }; /** * Time class that prints as hh:mm, can read in the same format and supports addition and subtraction of minutes. The time is represented as 24 hour time and any addition/subtraction is modulo 24:00. * @author Enoch Lau */ class Time { int minutes; /** The time in minutes, from the begnning of the day */ const static int MINUTES_IN_HOUR = 60; /** The number of minutes in an hour */ const static int MINUTES_IN_DAY = 1440; /** The number of minutes in a day, 60 * 24 */ /** * Parse a time * @param time The time in [hours]:[minutes] format, each of [hours] and [minutes] must be an integer (positive or negative) * @throw Time::BadTimeException if the format could not be parsed */ void parse_time(const std::string& time); public: typedef __BadTimeException BadTimeException; /** An exception that is thrown by the Time class for invalid formats */ /** * Time class constructor * @param m The time in minutes, from the begnning of the day */ Time(int m); /** * Time class constructor * @param h The number of whole hours to be included in the time * @param m The number of whole minutes to be included in the time */ Time(int h, int m); /** * Time class constructor * @param time The time in [hours]:[minutes] format, each of [hours] and [minutes] must be an integer (positive or negative) * @throw Time::BadTimeException if the format could not be parsed */ Time(const std::string& time); /** * Gets the time in hh:mm format * @return A pair, the first element being hours and the second element being minutes */ std::pair get_time() const; /** * Sets the time in hours and minutes * @param h The number of whole hours to be included in the time * @param m The number of whole minutes to be included in the time */ void set_time(int h, int m); /** * Gets the number of minutes from the beginning of the day * @return The number of minutes from the beginning of the day */ int get_total_minutes() const; /** * Sets the minutes from the beginning of the day * @param m The number of minutes from the beginning of the day */ void set_total_minutes(int m); /** * Adds a Time to another Time * @param t The right-hand operand * @return A Time with the combined number of minutes of the two Time objects, modulo 24 hours */ Time operator+(const Time& t) const; /** * Subtracts a Time from another Time * @param t The right-hand operand * @return A Time with the difference of the number of minutes of the two Time objects, modulo 24 hours */ Time operator-(const Time& t) const; /** * Adds a Time to this Time * @param t The right-hand operand * @return The left-hand operand */ Time& operator+=(const Time& t); /** * Subtracts a Time from this Time * @param t The right-hand operand * @return The left-hand operand */ Time& operator-=(const Time& t); /** * Converts the Time to a string in hh:mm form * @return The Time in hh:mm form */ std::string to_string() const; /** * Compares two Time objects to see if they represent the same time */ bool operator==(const Time& other) const; }; /** * Prints a Time to an output stream * @param out The output stream * @param t The Time to print */ std::ostream& operator<<(std::ostream& out, const Time& t); /** * Sets a Time by reading in a string from an input stream and attempting to parse it * @param in The input stream * @param t The Time where the time will be stored * @throw Time::BadTimeException if the format could not be parsed */ std::istream& operator>>(std::istream& in, Time& t); #endif