Created
November 18, 2015 06:41
-
-
Save yohhoy/a0860eb017da2fa95baf to your computer and use it in GitHub Desktop.
NTP Timestamp Format(64bit) to ISO 8601(extended format) + fraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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