Reputation: 837
Edited for solution.
This should be a trivial task, but it's taking me far too long and I am starting to doubt my sanity.
I have four std::string
s - hours, minutes, seconds, and milliseconds. I would like to get a ptime
out of them. At first I thought to convert the strings to numbers (using lexical_cast
?) and plug them in the ptime(date,time_duration)
constructor along with some dummy date (because I don't care for it), or maybe even with today's date. Then I figured it might be more efficient and less work if I just construct a single string and convert that to my ptime... but not only did I run into linking errors, I had to also fetch a gregorian::date
.
At this point I decided that maybe I am overthinking this. I could probably get one of those two methods to work after some time, but I feel like I am hacking something that should be way easier. Am I?
EDIT
Here's what I ended up doing. It's not as elegant as I wanted it to be, but I can settle for it working. For now.
using namespace boost::posix_time;
using namespace boost::gregorian;
std::string date_str = to_iso_extended_string( day_clock::local_day() ); // YYYY-MM-DD
std::string time_str( " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds); // HH:MM:SS:MMM
return time_from_string( date_str + time_str );
Upvotes: 0
Views: 1175
Reputation: 1621
I think you can do:
using namespace boost::posix_time;
long h = 1, m = 1, s = 1;//Get these from your lexical cast
ptime t(ptime(min_date_time) + hours(h) + minutes(m) + seconds(s));
std::cout << t << std::endl;//! output is 1400-Jan-01 01:01:01
The min_date_time assumes you don't care about the date (it uses the earliest representable date.)
Or you can use:
std::string ts("2002-01-20 23:59:59.000");
ptime t(time_from_string(ts))
Upvotes: 1