From cb33dd0f803dd20a08e163241c1f0f0efbca2f07 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 22 Nov 2021 11:26:38 +0100 Subject: [PATCH] Moved timestampConvert to own helper-file --- handler/textPacketHandler.hpp | 33 +------------------------------- helper/timestampConvert.hpp | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 helper/timestampConvert.hpp diff --git a/handler/textPacketHandler.hpp b/handler/textPacketHandler.hpp index dff2884..0d26562 100644 --- a/handler/textPacketHandler.hpp +++ b/handler/textPacketHandler.hpp @@ -8,38 +8,7 @@ #include #include #include "../helper/split.hpp" - -uint64_t convertStringToTimestampMicros(std::string textTimestamp){ - uint64_t timestamp; - - std::tm t = {}; - std::istringstream ssTimestamp = std::istringstream(textTimestamp); - if (ssTimestamp >> std::get_time(&t, "%H:%M:%S")) - { - // Get current time - std::time_t curT = std::time(0); - std::tm* curTime = std::localtime(&curT); - // Set missing fields - t.tm_mday = curTime->tm_mday; - t.tm_mon = curTime->tm_mon; - t.tm_year = curTime->tm_year; - t.tm_zone = curTime->tm_zone; - - // Convert tm to time - std::time_t time = std::mktime(&t); - - // Get micros - int micros = std::stoi(textTimestamp.substr(9, 6)); - - // Calculate timestamp epoch in micros - timestamp = time*1000000 + micros; - return timestamp; - } - else - { - throw std::runtime_error("Could not parse time: '"+ textTimestamp +"'"); - } -} +#include "../helper/timestampConvert.hpp" void textPacketHandler(std::vector textPacket){ /// Here we have to parse the packet diff --git a/helper/timestampConvert.hpp b/helper/timestampConvert.hpp new file mode 100644 index 0000000..b093ec6 --- /dev/null +++ b/helper/timestampConvert.hpp @@ -0,0 +1,36 @@ +#ifndef CC724CA7_8BB8_43B9_8A9A_54BD880A76AA +#define CC724CA7_8BB8_43B9_8A9A_54BD880A76AA + +uint64_t convertStringToTimestampMicros(std::string textTimestamp){ + uint64_t timestamp; + + std::tm t = {}; + std::istringstream ssTimestamp = std::istringstream(textTimestamp); + if (ssTimestamp >> std::get_time(&t, "%H:%M:%S")) + { + // Get current time + std::time_t curT = std::time(0); + std::tm* curTime = std::localtime(&curT); + // Set missing fields + t.tm_mday = curTime->tm_mday; + t.tm_mon = curTime->tm_mon; + t.tm_year = curTime->tm_year; + t.tm_zone = curTime->tm_zone; + + // Convert tm to time + std::time_t time = std::mktime(&t); + + // Get micros + int micros = std::stoi(textTimestamp.substr(9, 6)); + + // Calculate timestamp epoch in micros + timestamp = time*1000000 + micros; + return timestamp; + } + else + { + throw std::runtime_error("Could not parse time: '"+ textTimestamp +"'"); + } +} + +#endif /* CC724CA7_8BB8_43B9_8A9A_54BD880A76AA */