From 800ab74ea27ca22aa62e953cbc012989c23c34ca Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 9 Jan 2014 20:31:18 +0000 Subject: Basic implementation of a recorder --- p2pvr/ice/commonHelpers.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 p2pvr/ice/commonHelpers.cpp (limited to 'p2pvr/ice/commonHelpers.cpp') diff --git a/p2pvr/ice/commonHelpers.cpp b/p2pvr/ice/commonHelpers.cpp new file mode 100644 index 0000000..64a1c61 --- /dev/null +++ b/p2pvr/ice/commonHelpers.cpp @@ -0,0 +1,46 @@ +#include "commonHelpers.h" +#include +#include + +namespace Common { + std::string operator-(const Common::DateTime & a, const Common::DateTime & b) + { + struct tm tma { + 0, a.Minute, a.Hour, + a.Day, a.Month - 1, a.Year - 1900, + 0, 0, 0, 0, 0}; + struct tm tmb { + 0, b.Minute, b.Hour, + b.Day, b.Month - 1, b.Year - 1900, + 0, 0, 0, 0, 0}; + auto secs = mktime(&tma) - mktime(&tmb); + return stringbf("%02d:%02d:%02d", secs / 3600, (secs / 60) % 60, secs % 60); + } + + time_t operator-(const Common::DateTime & cdt, const std::string & interval) + { + struct tm dt { + 0, cdt.Minute, cdt.Hour, + cdt.Day, cdt.Month - 1, cdt.Year - 1900, + 0, 0, 0, 0, 0}; + unsigned short hours, minutes, seconds; + if (sscanf(interval.c_str(), "%hu:%hu:%hu", &hours, &minutes, &seconds) < 3) { + throw std::runtime_error("Couldn't parse interval"); + } + return mktime(&dt) - (seconds + (60 * (minutes + (60 * hours)))); + } + + time_t operator+(const Common::DateTime & cdt, const std::string & interval) + { + struct tm dt { + 0, cdt.Minute, cdt.Hour, + cdt.Day, cdt.Month - 1, cdt.Year - 1900, + 0, 0, 0, 0, 0}; + unsigned short hours, minutes, seconds; + if (sscanf(interval.c_str(), "%hu:%hu:%hu", &hours, &minutes, &seconds) < 3) { + throw std::runtime_error("Couldn't parse interval"); + } + return mktime(&dt) + (seconds + (60 * (minutes + (60 * hours)))); + } +} + -- cgit v1.2.3