Skip to content

Instantly share code, notes, and snippets.

@jiapengwen
Last active April 3, 2020 02:13
Show Gist options
  • Save jiapengwen/3cc51fe40d8c14adedd2fc59aeb5b1b9 to your computer and use it in GitHub Desktop.
Save jiapengwen/3cc51fe40d8c14adedd2fc59aeb5b1b9 to your computer and use it in GitHub Desktop.
c++ time utils
//*****************************************************
// ConvertYYYYMMDDhhmmss_us
//*****************************************************
static std::string ConvertYYYYMMDDhhmmss_us(uint64_t intime,
bool usegmtime = false)
{
char buff[50];
memset(buff, '\0', sizeof(buff));
std::string s_format = "%4d%02d%02d-%02d:%02d:%02d.%06d";
int nano = intime % 1000000000LL;
int micro = nano / 1000;
time_t epoch = (time_t)intime / 1000000000LL;
struct tm t;
if (usegmtime)
{
gmtime_r(&epoch, &t);
}
else
{
localtime_r(&epoch, &t);
}
sprintf(buff, s_format.c_str(), t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, micro);
return (std::string(buff));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment