String to increase posix_time and then again

No problem here, just curiosity. I am wondering how much code I had to write in order to achieve this, when a similar operation with other language implementations of DateTime will have only 3 or 4 lines.

So, I have a timestamp as a string, 06-Feb-2013 00:01:01 .: 06-Feb-2013 00:01:01

and using boost lib, I would create an input and output face, create a string stream and fill the io faces into the stream. Then I read the timestamp string in the string stream before transferring it to the posix time object to add 1 second to it before moving it back to the stream so that I can use it as a string. In a sense, everything I'm doing here turns a timestamp, like 06-Feb-2013 00:01:01 to 06-Feb-2013 00:01:02 .

An example would be:

 using namespace boost::posix_time; ptime timeStamp; time_facet* outFacet = new time_facet("%d-%b-%Y %H:%M:%S"); time_input_facet* inFacet = new time_input_facet("%d-%b-%Y %H:%M:%S"); std::stringstream ss; ss.imbue(std::locale(std::locale::classic(), outFacet)); ss.imbue(std::locale(ss.getloc(), inFacet)); ss.str(06-Feb-2013 00:01:01); ss >> timeStamp; timeStamp+=seconds(1); ss.str(""); ss << timeStamp; 

The code works, I have no problems with it. I am just wondering if I would do this in a circular fashion, considering in python / ruby ​​/ js, etc., All I had to do was something like:

 dt = new DateTime("06-Feb-2013 00:01:01", aformat); dt.addSecond() //or dt += another DateTime object dt.strformat(aformat) //or trace/print/puts dt 

Now I understand that C ++ is a lower level, but I still can't help but wonder. So, any improvements or thoughts?

+4
source share
1 answer

If you are ready to accept a slightly different (fixed) time format, use std :: string to_simple_string (ptime) (to format YYYY-mmm-DD HH: MM: SS.fffffffff string, where mmm 3 char month name Fractional seconds only include if non-zero, for http://www.boost.org/doc/libs/1_34_0/doc/html/date_time/posix_time.html ) can simplify your code to format the output along with the corresponding "ptime time_from_string (std: : string) ".

0
source

All Articles