Created
June 13, 2022 06:22
-
-
Save iamazeem/5ac5e68a210fc2e2770de0b75837f0e6 to your computer and use it in GitHub Desktop.
C++ timestamp [15 characters]
This file contains 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 <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