Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Created November 18, 2015 06:41
Show Gist options
  • Save yohhoy/a0860eb017da2fa95baf to your computer and use it in GitHub Desktop.
Save yohhoy/a0860eb017da2fa95baf to your computer and use it in GitHub Desktop.
NTP Timestamp Format(64bit) to ISO 8601(extended format) + fraction
std::string fmt_ntpts(uint64_t ts)
{
// 1970/1/1(UNIT time epoch) - 1900/1/1(NTP timestamp epoch)
const uint32_t offset = (24u * 60 * 60) * 25567;
time_t t = (ts >> 32) - offset;
double d = (ts & 0xffffffff) / (double)(1ULL << 32);
char buf[256];
size_t n = std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", std::gmtime(&t));
std::sprintf(buf + n, "%f", d);
buf[n] = ' '; // force erase '0'
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment