
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)
Originally posted by Martin Peris with karma: 5625 on 2019-12-26
This answer was NOT ACCEPTED on the original site
Post score: 3
Original comments
Comment by lucasw on 2019-12-28:
It compiles but crashes when the locale destructor tries to free
facet:
#3 0x00007ffff5cf390a in malloc_printerr (
str=str@entry=0x7ffff5e1b7a8 "munmap_chunk(): invalid pointer") at malloc.c:5350
#4 0x00007ffff5cfaecc in munmap_chunk (p=0x7fffffffb040) at malloc.c:2846
#5 __GI___libc_free (mem=0x7fffffffb050) at malloc.c:3117
#6 0x00007ffff6310834 in std::locale::_Impl::~_Impl() ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff63109e4 in std::locale::~locale() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
so this instead:
auto facet = new boost::posix_time::time_facet("%Y %m %d %H %M %s");
ss.imbue(std::locale(std::locale::classic(), facet));
(so it would be better to create facet once and reuse it).
https://stackoverflow.com/questions/17779660/who-is-responsible-for-deleting-the-facet
Comment by lucasw on 2019-12-28:
std::locale::classic and std::cout.getloc() are giving me UTC time, if that could be made into local time I'd prefer this answer.