I am working on code which needs to compile on NDK toolchain. Unfortunately, latest version only supports till gcc4.9 which does not support C++11 datetime parsing. I have a date time string which I need to send thru two-three formats to figure out parsing method.
So I tried linux API stftime which sometimes give values on wrong parsing method. I had to abandon it and move to boost.
Now coming on to boost I am using 1_64 version. According to the documentation here
I could not find a way to parse single digit hour format.
bool getepochtime(const std::string &str, const std::string &format, unsigned long &epoch){
epoch = 0;
namespace bt = boost::posix_time;
std::locale lformat = std::locale(std::locale::classic(), new bt::time_input_facet(format));
bt::ptime pt;
std::istringstream is(str);
is.imbue(lformat);
is >> pt;
if (pt == bt::ptime()) {
//epoch = 0;
return false;
}
bt::ptime timet_start(boost::gregorian::date(1970, 1, 1));
bt::time_duration diff = pt - timet_start;
epoch = (1000 * diff.ticks()/bt::time_duration::rep_type::ticks_per_second);
return true;
}
int main() {
unsigned long eval;
// this works.
getepochtime("28th january 11:50 PM", "%dth %B %H:%M %p", eval);
// this does not work.
getepochtime("28th january 1:50 PM", "%dth %B %I:%M %p", eval);
// nor this.
getepochtime("28th january 1:50 PM", "%dth %B %H:%M %p", eval);
return 0;
}
Any help will be appreciated.
I'll leave it to you to sort out how you want dates without years to be interpreted. However, here's a quick start using /just/
strptime
.I used it in a larger codebase, and we needed some pretty versatile date recognition. Behold: the adaptive datetime parser:
You can use it as such:
Live On Wandbox
Printing:
Implementation
The implementation comes with a bunch of pretty well-tuned unambiguous date patterns. Our project used a number of "hacks" to normalize strange input formats, these have been omitted (you can see the commented references to
detail::normalize_...
functions for ideas):