Example code for ISO 8601 duration to Unix epoch time converter:
#include <iostream>
#include <vector>
#include <regex>
using namespace std;
void match_duration(const std::string& input, const std::regex& re)
{
std::smatch match;
std::regex_search(input, match, re);
if (match.empty()) {
std::cout << "Pattern do NOT match" << std::endl;
return;
}
std::vector<double> vec = {0,0,0,0,0,0}; // years, months, days, hours, minutes, seconds
for (size_t i = 1; i < match.size(); ++i) {
if (match[i].matched) {
std::string str = match[i];
str.pop_back(); // remove last character.
vec[i-1] = std::stod(str);
}
}
int duration = 31556926 * vec[0] + // years
2629743.83 * vec[1] + // months
86400 * vec[2] + // days
3600 * vec[3] + // hours
60 * vec[4] + // minutes
1 * vec[5]; // seconds
if (duration == 0) {
std::cout << "Not valid input" << std::endl;
return;
}
std::cout << "duration: " << duration << " [sec.]" << std::endl;
}
int main()
{
std::cout << "-- ISO 8601 duration to Unix epoch time converter--" << std::endl;
std::cout << "Enter duration (q for quit)" << std::endl;
std::string input;
//input = "P1Y2M3DT4H5M6S";
//input = "PT4H5M6S";
//
while(true)
{
std::cin >> input;
if (!std::cin)
break;
if (input == "q")
break;
std::regex rshort("^((?!T).)*$");
if (std::regex_match(input, rshort)) // no T (Time) exist
{
std::regex r("P([[:d:]]+Y)?([[:d:]]+M)?([[:d:]]+D)?");
match_duration(input, r);
}
else {
std::regex r("P([[:d:]]+Y)?([[:d:]]+M)?([[:d:]]+D)?T([[:d:]]+H)?([[:d:]]+M)?([[:d:]]+S|[[:d:]]+\\.[[:d:]]+S)?");
match_duration(input, r);
}
}
return 0;
}
P2M
meaning either 59 days (5,097,600 seconds) or 62 days (5,356,800 seconds) depending on when it starts. – Coccid