Skip to content

Instantly share code, notes, and snippets.

@iamazeem
Created June 13, 2022 06:22
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 iamazeem/5ac5e68a210fc2e2770de0b75837f0e6 to your computer and use it in GitHub Desktop.
Save iamazeem/5ac5e68a210fc2e2770de0b75837f0e6 to your computer and use it in GitHub Desktop.
C++ timestamp [15 characters]
#include <iostream>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <ctime>
std::string get_timestamp() noexcept
{
const auto now = std::chrono::system_clock::now();
const auto time = std::chrono::system_clock::to_time_t(now);
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) -
std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
std::ostringstream oss;
oss << std::put_time(std::localtime(&time), "%d%m%yT%H%M%S") << ms.count();
return oss.str();
}
int main()
{
std::cout << get_timestamp();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment