I want to get year-month-day hour:minute:second.fraction(2 digits), if I use "%Y-%m-%d %H:%M:%S.%f", I got almost what I want exception for the fraction( last part ) of seconds, it's showing 6 digits on my Windows XP, I don't know how to get 2 digits only, any idea?
I'm using this
namespace boost
{
BOOST_LOG_OPEN_NAMESPACE
namespace attributes
{
template <typename date_time_type>
void format_time_ms( std::ostringstream& formatter, const date_time_type& date_time)
{
auto time = date_time.time_of_day();
using namespace std;
formatter
<< setfill( '0') << setw( 2) << time.hours() << ':'
<< setfill( '0') << setw( 2) << time.minutes() << ':'
<< setfill( '0') << setw( 2) << time.seconds() << ','
<< setfill( '0') << setw( 3) << time.fractional_seconds() / 1000;
}
template <typename date_time_type>
std::string format_time_ms( const date_time_type& date_time)
{
std::ostringstream formatter;
format_time_ms( formatter, date_time);
auto time = date_time.time_of_day();
return formatter.str();
}
template <typename date_time_type>
std::string format_date_time_ms( const date_time_type& date_time, const char date_time_sep = ' ')
{
using namespace std;
ostringstream formatter;
auto date = date_time.date();
formatter
<< date.year() << '-'
<< setfill( '0') << setw( 2) << int( date.month()) << '-'
<< setfill( '0') << setw( 2) << date.day() << date_time_sep;
format_time_ms( formatter, date_time);
return formatter.str();
}
template <typename date_time_type, const char date_time_sep = ' '>
struct date_time_ms_formatter
{
std::string operator () ( const date_time_type& date_time) { return format_date_time_ms( date_time, date_time_sep); }
};
struct time_ms_formatter
{
template <typename date_time_type>
std::string operator () ( const date_time_type& date_time) { return format_time_ms( date_time); }
};
template <typename time_type>
struct local_clock_source
{
time_type operator () () const
{
return date_time::microsec_clock<time_type>::local_time();
}
};
template <typename time_type>
struct universal_clock_source
{
time_type operator () () const
{
return date_time::microsec_clock<time_type>::universal_time();
}
};
template <typename time_type, typename clock_source_type, typename formater_type>
class custom_clock: public attribute
{
public:
class impl: public attribute::impl
{
public:
attribute_value get_value()
{
auto str = formater_type()( clock_source_type()());
return make_attribute_value( str);
}
};
custom_clock(): attribute( new impl()) {}
explicit custom_clock( const cast_source& source): attribute( source.as<impl>()) {}
};
typedef custom_clock<boost::posix_time::ptime, local_clock_source<boost::posix_time::ptime>, date_time_ms_formatter<boost::posix_time::ptime, '\t'> > local_date_time_ms_clock;
typedef custom_clock<boost::posix_time::ptime, universal_clock_source<boost::posix_time::ptime>, date_time_ms_formatter<boost::posix_time::ptime, '\t'> > universal_date_time_ms_clock;
typedef custom_clock<boost::posix_time::ptime, local_clock_source<boost::posix_time::ptime>, time_ms_formatter> local_time_ms_clock;
typedef custom_clock<boost::posix_time::ptime, universal_clock_source<boost::posix_time::ptime>, time_ms_formatter> universal_time_ms_clock;
}
BOOST_LOG_CLOSE_NAMESPACE // namespace log
}
initialized as
BOOST_LOG_ATTRIBUTE_KEYWORD( dateTimeStamp, "DateTime", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD( timeStamp, "Time", boost::posix_time::ptime)
core->add_global_attribute( dateTimeStamp.get_name(), attrs::local_date_time_ms_clock());
core->add_global_attribute( timeStamp.get_name(), attrs::local_time_ms_clock());
and used as
expr::stream << expr::attr<std::string>( dateTimeStamp.get_name())
expr::stream << expr::attr<std::string>( timeStamp.get_name())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With