Add utility functions TimePointToString and SystemTimePointToString

This commit is contained in:
Isaac Connor 2021-12-16 16:30:26 -05:00
parent 7c3ab46408
commit 7b7ac8fc7e
3 changed files with 57 additions and 0 deletions

View File

@ -60,6 +60,7 @@ set(ZM_BIN_SRC_FILES
zm_signal.cpp zm_signal.cpp
zm_stream.cpp zm_stream.cpp
zm_swscale.cpp zm_swscale.cpp
zm_time.cpp
zm_user.cpp zm_user.cpp
zm_utils.cpp zm_utils.cpp
zm_videostore.cpp zm_videostore.cpp

52
src/zm_time.cpp Normal file
View File

@ -0,0 +1,52 @@
//
// 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 <cinttypes>
std::string SystemTimePointToString(SystemTimePoint tp) {
time_t tp_sec = std::chrono::system_clock::to_time_t(tp);
Microseconds now_frac = std::chrono::duration_cast<Microseconds>(
tp.time_since_epoch() - std::chrono::duration_cast<Seconds>(tp.time_since_epoch()));
std::string timeString;
timeString.reserve(64);
char *timePtr = timeString.data();
tm tp_tm = {};
timePtr += strftime(timePtr, timeString.capacity(), "%x %H:%M:%S", localtime_r(&tp_sec, &tp_tm));
snprintf(timePtr, timeString.capacity() - (timePtr - timeString.data()), ".%06" PRIi64, static_cast<int64_t>(now_frac.count()));
return timeString;
}
std::string TimePointToString(TimePoint tp) {
time_t tp_sec = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now() + (tp - std::chrono::steady_clock::now()));
Microseconds now_frac = std::chrono::duration_cast<Microseconds>(
tp.time_since_epoch() - std::chrono::duration_cast<Seconds>(tp.time_since_epoch()));
std::string timeString;
timeString.reserve(64);
char *timePtr = timeString.data();
tm tp_tm = {};
timePtr += strftime(timePtr, timeString.capacity(), "%x %H:%M:%S", localtime_r(&tp_sec, &tp_tm));
snprintf(timePtr, timeString.capacity() - (timePtr - timeString.data()), ".%06" PRIi64, static_cast<int64_t>(now_frac.count()));
return timeString;
}

View File

@ -21,6 +21,7 @@
#define ZM_TIME_H #define ZM_TIME_H
#include <chrono> #include <chrono>
#include <string>
#include <sys/time.h> #include <sys/time.h>
typedef std::chrono::microseconds Microseconds; typedef std::chrono::microseconds Microseconds;
@ -120,4 +121,7 @@ class TimeSegmentAdder {
bool finished_; bool finished_;
}; };
std::string SystemTimePointToString(SystemTimePoint tp);
std::string TimePointToString(TimePoint tp);
#endif // ZM_TIME_H #endif // ZM_TIME_H