Skip to content

Instantly share code, notes, and snippets.

@1ace
Created May 19, 2015 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1ace/a66b75eeafc200c14280 to your computer and use it in GitHub Desktop.
Save 1ace/a66b75eeafc200c14280 to your computer and use it in GitHub Desktop.
Unix timestamp with microsecond precision
#include <chrono> // std::chrono::high_resolution_clock, std::chrono::duration
#include <iostream> // std::cout
#include <iomanip> // std::fixed
int main() {
auto now = std::chrono::high_resolution_clock::now();
auto timestamp = std::chrono::duration<double>(now.time_since_epoch()).count();
std::cout << std::fixed << timestamp << std::endl;
}
@1ace
Copy link
Author

1ace commented May 19, 2015

This code uses the chrono library, which is part of C++11. You may need to add the compile flag -std=c++11 (or equivalent).

@1ace
Copy link
Author

1ace commented May 19, 2015

As an explanation, I made this to be able to track time in shell scripts with more precision than date +%s

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