Вы можете использовать boost
библиотеку для управления временем.
Посмотрите на boost :: date_time :: parse_date (const std :: string & s, int order_spe c = ymd_order_iso) :
//! Generic function to parse a delimited date (eg: 2002-02-10)
/*! Accepted formats are: "2003-02-10" or " 2003-Feb-10" or
* "2003-Feburary-10"
* The order in which the Month, Day, & Year appear in the argument
* string can be accomodated by passing in the appropriate ymd_order_spec
*/
И при boost :: date_time :: parse_delimited_time_duration (const std :: string & s) :
//! Creates a time_duration object from a delimited string
/*! Expected format for string is "[-]h[h][:mm][:ss][.fff]".
* If the number of fractional digits provided is greater than the
* precision of the time duration type then the extra digits are
* truncated.
*
* A negative duration will be created if the first character in
* string is a '-', all other '-' will be treated as delimiters.
* Accepted delimiters are "-:,.".
*/
Boost сделал функцию-обертку для оба эти метода могут анализировать строку даты и времени:
template<class time_type>
inline time_type parse_delimited_time(const std::string& s, char sep) {
typedef typename time_type::time_duration_type time_duration;
typedef typename time_type::date_type date_type;
//split date/time on a unique delimiter char such as ' ' or 'T'
std::string date_string, tod_string;
split(s, sep, date_string, tod_string);
//call parse_date with first string
date_type d = parse_date<date_type>(date_string);
//call parse_time_duration with remaining string
time_duration td = parse_delimited_time_duration<time_duration>(tod_string);
//construct a time
return time_type(d, td);
}
Если вам нужен другой анализатор формата даты, я сделал несколько иную реализацию:
posix_time::ptime parse_dmy_time(const std::string &s, char sep) {
typedef typename posix_time::ptime::time_duration_type time_duration;
typedef typename posix_time::ptime::date_type date_type;
//split date/time on a unique delimiter char such as ' ' or 'T'
std::string date_string, tod_string;
split(s, sep, date_string, tod_string);
//call parse_date with first string
auto d = parse_date<date_type>(date_string, ymd_order_dmy);
//call parse_time_duration with remaining string
auto td = parse_delimited_time_duration<time_duration>(tod_string);
//construct a time
return {d, td};
}