time.cpp (1835B)
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 #include "time.hpp" 19 20 #include <array> 21 #include <iomanip> 22 #include <sstream> 23 24 Time::Time(const char *iso8601) : m_tm() 25 { 26 tm time = {}; 27 28 std::istringstream stream(iso8601); 29 stream >> std::get_time(&time, "%Y-%m-%dT%H:%M:%S"); 30 31 if(stream.good()) 32 std::swap(m_tm, time); 33 } 34 35 Time::Time(int year, int month, int day, int hour, int minute, int second) 36 : m_tm() 37 { 38 m_tm.tm_year = year - 1900; 39 m_tm.tm_mon = month - 1; 40 m_tm.tm_mday = day; 41 42 m_tm.tm_hour = hour; 43 m_tm.tm_min = minute; 44 m_tm.tm_sec = second; 45 } 46 47 std::string Time::toString() const 48 { 49 if(!isValid()) 50 return {}; 51 52 char buf[32] = {}; 53 strftime(buf, sizeof(buf), "%B %d %Y", &m_tm); 54 return buf; 55 } 56 57 int Time::compare(const Time &o) const 58 { 59 const std::array<int, 6> l{year(), month(), day(), hour(), minute(), second()}; 60 const std::array<int, 6> r{o.year(), o.month(), o.day(), o.hour(), o.minute(), o.second()}; 61 62 if(l < r) 63 return -1; 64 else if(l > r) 65 return 1; 66 67 return 0; 68 } 69 70 std::ostream &operator<<(std::ostream &os, const Time &time) 71 { 72 os << time.toString(); 73 return os; 74 }