#ifndef ISO_8601_DURATION_PARSER_HPP #define ISO_8601_DURATION_PARSER_HPP #include #include #include namespace osrm { namespace util { namespace qi = boost::spirit::qi; template struct iso_8601_grammar : qi::grammar { iso_8601_grammar() : iso_8601_grammar::base_type(iso_period), temp(0), hours(0), minutes(0), seconds(0) { iso_period = qi::lit('P') >> qi::lit('T') >> ((value >> hour >> value >> minute >> value >> second) | (value >> hour >> value >> minute) | (value >> hour >> value >> second) | (value >> hour) | (value >> minute >> value >> second) | (value >> minute) | (value >> second)); value = qi::uint_[boost::bind(&iso_8601_grammar::set_temp, this, ::_1)]; second = (qi::lit('s') | qi::lit('S'))[boost::bind(&iso_8601_grammar::set_seconds, this)]; minute = (qi::lit('m') | qi::lit('M'))[boost::bind(&iso_8601_grammar::set_minutes, this)]; hour = (qi::lit('h') | qi::lit('H'))[boost::bind(&iso_8601_grammar::set_hours, this)]; } qi::rule iso_period; qi::rule value, hour, minute, second; unsigned temp; unsigned hours; unsigned minutes; unsigned seconds; void set_temp(unsigned number) { temp = number; } void set_hours() { if (temp < 24) { hours = temp; } } void set_minutes() { if (temp < 60) { minutes = temp; } } void set_seconds() { if (temp < 60) { seconds = temp; } } unsigned get_duration() const { unsigned temp = (3600 * hours + 60 * minutes + seconds); if (temp == 0) { temp = std::numeric_limits::max(); } return temp; } }; } } #endif // ISO_8601_DURATION_PARSER_HPP