Using boost parse datetime string: With single digit hour format

Viewed 2015

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.

1 Answers
Related