Frage

I figured out how to get the year, month, and date using boost::posix_time(). However I cannot figure out how to get the current hours or minutes.

      pt::ptime now = pt::second_clock::local_time();
  std::stringstream ss;
  ss << static_cast<int>(now.date().month()) << "/" << now.date().day()
      << "/" << now.date().year();

There doesn't appear to be any now.hours options. Is there another library I should use

War es hilfreich?

Lösung

What about using std::chrono?

#include <iostream>
#include <chrono>

using namespace std::chrono;

int main(int argc, char* argv[])
{
   system_clock::time_point p = system_clock::now();
   auto t = system_clock::to_time_t(p);

   std::cout << std::ctime(&t) << std::endl; // e.g Tue Dec 27 17:21:29 2011

   return 0;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top