ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

The way you control the output format of a boost::posix_time object is by constructing a facet with the format flags that you want and apply the facet to a std::stringstream used to generate the string. It will be clearer with an example:

#include <iostream>
#include <locale>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <ros/ros.h>
...
auto current_time = ros::Time::now().toBoost(); //You asked for modern C++, so... there :P
std::stringstream ss;
boost::posix_time::time_facet facet("%Y %m %d %H %M %s");
ss.imbue(std::locale(std::locale::classic(), &facet));
ss << current_time;
ROS_INFO("Current time: %s", ss.str().c_str());

I hope you find this helpful. (Did this on-the-fly, so this code might not compile, but should put you on the right track)