Skip to content

Instantly share code, notes, and snippets.

@dtoma
Created December 29, 2016 08:07
Show Gist options
  • Save dtoma/cc3d5b2dd4c03a25886adededcc1e3b9 to your computer and use it in GitHub Desktop.
Save dtoma/cc3d5b2dd4c03a25886adededcc1e3b9 to your computer and use it in GitHub Desktop.
C++ timestamp with nanoseconds and timezone
#include <chrono>
#include <iomanip>
#include <iostream>
int main() {
auto now = std::chrono::high_resolution_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
auto tm = *std::gmtime(&time);
// auto tm = *std::localtime(&time);
auto epoch = now.time_since_epoch();
auto us = std::chrono::duration_cast<std::chrono::microseconds>(epoch).count() % 1000000;
std::cout << std::put_time(&tm, "%F %T.") << us << std::put_time(&tm, " %Z\n");
}
@dtoma
Copy link
Author

dtoma commented Dec 29, 2016

Example:

$> g++ -Wall -Wextra -pedantic -Wconversion -O3 ts.cpp && ./a.out
2016-12-29 08:06:46.407077 GMT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment