-
-
Save williamlfang/ada534c74c14c443ebd7b9d4c3cae188 to your computer and use it in GitHub Desktop.
C++ timestamp with nanoseconds and timezone
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
| #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