据我所知,Boost.DateTime 没有内置此功能,但编写自己的格式化函数非常简单,例如:
template<typename CharT, typename TraitsT>
std::basic_ostream<CharT, TraitsT>& print_date(
std::basic_ostream<CharT, TraitsT>& os,
boost::posix_time::ptime const& pt)
{
boost::gregorian::date const& d = pt.date();
return os
<< d.month().as_number() << '/'
<< d.day().as_number() << '/'
<< d.year();
}
template<typename CharT, typename TraitsT>
std::basic_ostream<CharT, TraitsT>& print_date_time(
std::basic_ostream<CharT, TraitsT>& os,
boost::posix_time::ptime const& pt)
{
boost::gregorian::date const& d = pt.date();
boost::posix_time::time_duration const& t = pt.time_of_day();
CharT const orig_fill(os.fill('0'));
os
<< d.month().as_number() << '/'
<< d.day().as_number() << '/'
<< d.year() << ' '
<< (t.hours() && t.hours() != 12 ? t.hours() % 12 : 12) << ':'
<< std::setw(2) << t.minutes() << ':'
<< std::setw(2) << t.seconds() << ' '
<< (t.hours() / 12 ? 'P' : 'A') << 'M';
os.fill(orig_fill);
return os;
}