Skip to content

Instantly share code, notes, and snippets.

@andrewcrabb
Last active January 4, 2019 18:18
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 andrewcrabb/76caaac2e6411a274d13e0d90703ce25 to your computer and use it in GitHub Desktop.
Save andrewcrabb/76caaac2e6411a274d13e0d90703ce25 to your computer and use it in GitHub Desktop.
Boost datetime to formatted output modified from http://bit.ly/2VwaARa http://cpp.sh/6mp4m
#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>
#include <sstream>
std::string FormatTime(boost::posix_time::ptime now)
{
using namespace boost::posix_time;
static std::locale loc(std::wcout.getloc(),
new wtime_facet(L"%Y%m%d_%H%M%S"));
std::stringstream wss;
wss.imbue(loc);
wss << now;
return wss.str();
}
int main() {
using namespace boost::posix_time;
ptime now = second_clock::universal_time();
std::string ws(FormatTime(now));
std::cout << ws << std::endl;
sleep(2);
now = second_clock::universal_time();
ws = FormatTime(now);
std::cout << ws << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment