Add UTC string parsing functions
Change-Id: I51cbb2274e26811d28b303375ff75a32e272adcc
This commit is contained in:
parent
62b14d958d
commit
00d9b66602
2 changed files with 35 additions and 0 deletions
|
@ -6,6 +6,8 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sys/time.h> //for gettimeofday
|
#include <sys/time.h> //for gettimeofday
|
||||||
#include <time.h> //for time and nanosleep
|
#include <time.h> //for time and nanosleep
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// emulate clock_gettime() for OSX compatibility
|
// emulate clock_gettime() for OSX compatibility
|
||||||
#if defined(__APPLE__) || defined(__MACH__)
|
#if defined(__APPLE__) || defined(__MACH__)
|
||||||
|
@ -122,6 +124,37 @@ std::string Util::getUTCStringMillis(uint64_t epoch_millis){
|
||||||
return std::string(result);
|
return std::string(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the epoch of a UTC string in the format of %Y-%m-%dT%H:%M:%S%z
|
||||||
|
uint64_t Util::getMSFromUTCString(std::string UTCString){
|
||||||
|
if (UTCString.size() < 24){return 0;}
|
||||||
|
// Strip milliseconds
|
||||||
|
std::string millis = UTCString.substr(UTCString.rfind('.') + 1, 3);
|
||||||
|
UTCString = UTCString.substr(0, UTCString.rfind('.')) + "Z";
|
||||||
|
struct tm ptm;
|
||||||
|
memset(&ptm, 0, sizeof(struct tm));
|
||||||
|
strptime(UTCString.c_str(), "%Y-%m-%dT%H:%M:%S%z", &ptm);
|
||||||
|
time_t ts = mktime(&ptm);
|
||||||
|
return ts * 1000 + atoll(millis.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts epoch_millis into UTC time and returns the diff with UTCString in seconds
|
||||||
|
uint64_t Util::getUTCTimeDiff(std::string UTCString, uint64_t epochMillis){
|
||||||
|
if (!epochMillis){return 0;}
|
||||||
|
if (UTCString.size() < 24){return 0;}
|
||||||
|
// Convert epoch to UTC time
|
||||||
|
time_t epochSeconds = epochMillis / 1000;
|
||||||
|
struct tm *ptmEpoch;
|
||||||
|
ptmEpoch = gmtime(&epochSeconds);
|
||||||
|
uint64_t epochTime = mktime(ptmEpoch);
|
||||||
|
// Parse UTC string and strip the milliseconds
|
||||||
|
UTCString = UTCString.substr(0, UTCString.rfind('.')) + "Z";
|
||||||
|
struct tm ptmUTC;
|
||||||
|
memset(&ptmUTC, 0, sizeof(struct tm));
|
||||||
|
strptime(UTCString.c_str(), "%Y-%m-%dT%H:%M:%S%z", &ptmUTC);
|
||||||
|
time_t UTCTime = mktime(&ptmUTC);
|
||||||
|
return epochTime - UTCTime;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Util::getDateString(uint64_t epoch){
|
std::string Util::getDateString(uint64_t epoch){
|
||||||
char buffer[80];
|
char buffer[80];
|
||||||
time_t rawtime = epoch;
|
time_t rawtime = epoch;
|
||||||
|
|
|
@ -18,5 +18,7 @@ namespace Util{
|
||||||
uint64_t epoch(); ///< Gets the amount of seconds since 01/01/1970.
|
uint64_t epoch(); ///< Gets the amount of seconds since 01/01/1970.
|
||||||
std::string getUTCString(uint64_t epoch = 0);
|
std::string getUTCString(uint64_t epoch = 0);
|
||||||
std::string getUTCStringMillis(uint64_t epoch_millis = 0);
|
std::string getUTCStringMillis(uint64_t epoch_millis = 0);
|
||||||
|
uint64_t getMSFromUTCString(std::string UTCString);
|
||||||
|
uint64_t getUTCTimeDiff(std::string UTCString, uint64_t epochMillis);
|
||||||
std::string getDateString(uint64_t epoch = 0);
|
std::string getDateString(uint64_t epoch = 0);
|
||||||
}// namespace Util
|
}// namespace Util
|
||||||
|
|
Loading…
Add table
Reference in a new issue