Skip to content

Instantly share code, notes, and snippets.

@williamlfang
Forked from dtoma/ts.cpp
Created March 20, 2020 16:49
Show Gist options
  • Select an option

  • Save williamlfang/ada534c74c14c443ebd7b9d4c3cae188 to your computer and use it in GitHub Desktop.

Select an option

Save williamlfang/ada534c74c14c443ebd7b9d4c3cae188 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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment