time.hpp (1969B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_TIME_HPP 19 #define REAPACK_TIME_HPP 20 21 #include <ctime> 22 #include <string> 23 24 class Time { 25 public: 26 Time(const char *iso8601); 27 Time(int year, int month, int day, int hour = 0, int minute = 0, int second = 0); 28 Time(const std::tm &tm = {}) : m_tm(tm) {} 29 30 bool isValid() const { return m_tm.tm_year > 0; } 31 operator bool() const { return isValid(); } 32 33 int year() const { return m_tm.tm_year + 1900; } 34 int month() const { return m_tm.tm_mon + 1; } 35 int day() const { return m_tm.tm_mday; } 36 int hour() const { return m_tm.tm_hour; } 37 int minute() const { return m_tm.tm_min; } 38 int second() const { return m_tm.tm_sec; } 39 40 std::string toString() const; 41 42 int compare(const Time &) const; 43 bool operator<(const Time &o) const { return compare(o) < 0; } 44 bool operator<=(const Time &o) const { return compare(o) <= 0; } 45 bool operator>(const Time &o) const { return compare(o) > 0; } 46 bool operator>=(const Time &o) const { return compare(o) >= 0; } 47 bool operator==(const Time &o) const { return compare(o) == 0; } 48 bool operator!=(const Time &o) const { return compare(o) != 0; } 49 50 private: 51 std::tm m_tm; 52 }; 53 54 std::ostream &operator<<(std::ostream &os, const Time &time); 55 56 #endif