Skip to content

Instantly share code, notes, and snippets.

@haolly
Forked from bschlinker/timestampWithMs.cpp
Created September 28, 2022 10:02
Show Gist options
  • Save haolly/85e9226a6105bc5fd18728d50be7e228 to your computer and use it in GitHub Desktop.
Save haolly/85e9226a6105bc5fd18728d50be7e228 to your computer and use it in GitHub Desktop.
Timestamp with milliseconds
#include <chrono>
#include <iomanip>
#include <iostream>
string getTimestamp() {
// get a precise timestamp as a string
const auto now = std::chrono::system_clock::now();
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;
std::stringstream nowSs;
nowSs
<< std::put_time(std::localtime(&nowAsTimeT), "%a %b %d %Y %T")
<< '.' << std::setfill('0') << std::setw(3) << nowMs.count();
return nowSs.str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment