add more time formats

pull/141/head
bert hubert 2020-09-27 16:12:39 +02:00
parent bccc3eb63a
commit 386ad8dd7d
2 changed files with 36 additions and 3 deletions

View File

@ -159,6 +159,18 @@ std::string humanTimeShort(time_t t)
return buffer;
}
// influx ascii time format, in UTC
std::string influxTime(time_t t)
{
struct tm tm={0};
gmtime_r(&t, &tm);
char buffer[80];
std::string fmt = "%Y-%m-%d %H:%M:%S";
strftime(buffer, sizeof(buffer), fmt.c_str(), &tm);
return buffer;
}
std::string humanTime(time_t t, uint32_t nanoseconds)
{
@ -388,7 +400,7 @@ time_t parseTime(std::string_view in)
{
time_t now=time(0);
vector<string> formats({"%Y-%m-%d %H:%M", "%Y%m%d %H%M", "%H:%M", "%H%M"});
vector<string> formats({"%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S", "%Y%m%d %H%M", "%H:%M", "%H%M"});
for(const auto& f : formats) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
@ -405,7 +417,24 @@ time_t parseTime(std::string_view in)
string err="Can only parse on";
for(const auto& f : formats)
err += " "+ f;
err += " '"+ f+"'";
throw runtime_error(err);
}
std::string string_replace(const std::string& str, const std::string& match,
const std::string& replacement, unsigned int max_replacements)
{
size_t pos = 0;
std::string newstr = str;
unsigned int replacements = 0;
while ((pos = newstr.find(match, pos)) != std::string::npos
&& replacements < max_replacements)
{
newstr.replace(pos, match.length(), replacement);
pos += replacement.length();
replacements++;
}
return newstr;
}

View File

@ -5,7 +5,7 @@
#include <string>
#include <tuple>
#include <mutex>
#include <limits.h>
extern const char* g_gitHash;
@ -18,6 +18,8 @@ std::string humanTimeNow();
std::string humanTime(time_t t);
std::string humanTimeShort(time_t t);
std::string humanTime(time_t t, uint32_t nanoseconds);
// influx ascii time format, in UTC
std::string influxTime(time_t t);
struct SatID
{
uint32_t gnss{255}; // these could all be 'int16_t' but leads to howling numbers of warnings with protobuf
@ -85,3 +87,5 @@ std::string makeHexDump(const std::basic_string<uint8_t>& str);
size_t writen2(int fd, const void *buf, size_t count);
void unixDie(const std::string& reason);
time_t parseTime(std::string_view in);
std::string string_replace(const std::string& str, const std::string& match,
const std::string& replacement, unsigned int max_replacements = UINT_MAX);