tags:

views:

227

answers:

3

Hi guys, I didn't find this in documentation: how to get local time (better formatted) with boost?

A: 

Use the Date Time Input/Output APIs:

http://www.boost.org/doc/libs/1_42_0/doc/html/date_time/date_time_io.html#date_time.io_objects

jsight
There is a lot of samples for the converting input data, but not about getting that data from system information.
Ockonal
+1  A: 

I don't know if this will be of any help, but boost docs have some examples of formatting dates.

Plus, I think that this article describes some basics, which are worth looking at.

zeroDivisible
+1  A: 

Use posix_time to construct a time object from the system clock.

For example, this would output the current system time as an ISO-format string:

namespace pt = boost::posix_time;
pt::to_iso_string(pt::second_clock::local_time());

For formatting alternatives, see the “Conversion to String” section of the above-linked reference and the Date Time Input/Output reference. Alternatively, you can build your own output string using the accessor functions. For example, to get a US-style date:

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

Note the month is cast to int so it will display as digits. The default output facet will display it as the three-letter month abbreviation (“Mar” for March).

Nate