// // ZoneMinder Time Functions & Definitions, $Date$, $Revision$ // Copyright (C) 2001-2008 Philip Coombes // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // #include "zm_time.h" #include #include std::string SystemTimePointToString(SystemTimePoint tp) { time_t tp_sec = std::chrono::system_clock::to_time_t(tp); Microseconds now_frac = std::chrono::duration_cast( tp.time_since_epoch() - std::chrono::duration_cast(tp.time_since_epoch())); // Format into a real buffer: writing into a string's spare capacity leaves its size at 0. char buffer[96]; tm tp_tm = {}; size_t len = strftime(buffer, sizeof(buffer), "%x %H:%M:%S", localtime_r(&tp_sec, &tp_tm)); snprintf(buffer + len, sizeof(buffer) - len, ".%06" PRIi64, static_cast(now_frac.count())); return std::string(buffer); } std::string SystemTimePointToMysqlString(SystemTimePoint tp) { // Format to whole-second precision only. The MySQL datetime columns this feeds // (e.g. Events.StartDateTime) have no fractional precision, and MySQL 8 ROUNDS // a fractional string when storing it, so ".999999" before midnight would be // promoted to 00:00:00 of the next day. Event::SetPath() derives the on-disk // day folder from to_time_t() (which truncates), so emitting a roundable // fractional made the DB row and the disk folder diverge by a day for events // starting just before local midnight. Truncate here to match to_time_t and // keep both representations on the same second. refs #4870 time_t tp_sec = std::chrono::system_clock::to_time_t(tp); char buffer[32]; tm tp_tm = {}; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime_r(&tp_sec, &tp_tm)); return std::string(buffer); } std::string TimePointToString(TimePoint tp) { const auto tp_dur = std::chrono::duration_cast(tp - std::chrono::steady_clock::now()); time_t tp_sec = std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() + tp_dur); Microseconds now_frac = std::chrono::duration_cast( tp.time_since_epoch() - std::chrono::duration_cast(tp.time_since_epoch())); // Format into a real buffer: writing into a string's spare capacity leaves its size at 0. char buffer[96]; tm tp_tm = {}; size_t len = strftime(buffer, sizeof(buffer), "%x %H:%M:%S", localtime_r(&tp_sec, &tp_tm)); snprintf(buffer + len, sizeof(buffer) - len, ".%06" PRIi64, static_cast(now_frac.count())); return std::string(buffer); } std::string format_absolute_time_iso8601(time_t time) { struct tm *tm_utc = gmtime(&time); if (!tm_utc) { return ""; } char buffer[32]; strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S.000Z", tm_utc); return std::string(buffer); } SystemTimePoint StringToSystemTimePoint(const std::string ×tamp) { std::tm t{}; if (!strptime(timestamp.c_str(), "%Y-%m-%d %H:%M:%S", &t)) { // Empty (NULL column) or unparseable: callers compare against the epoch to // see whether a time was ever set, not a mktime() of an all zero tm. return SystemTimePoint(); } t.tm_isdst = -1; // let mktime decide, otherwise summer times are read as standard time time_t time_t_val = mktime(&t); SystemTimePoint stp = std::chrono::system_clock::from_time_t(time_t_val); return stp; }