如何在C ++ 03中解析RFC3339时间戳(“1985-04-12T23:20:50.52Z”)(即ISO8601的子集)?我正在使用Boost,但没有一个Boost日期时间库似乎包含一个执行此操作的函数。
实际时间对象的类型无关紧要,只要我可以轻松地将它与“现在”进行比较。我只关心UTC时区的时间戳。
答案 0 :(得分:1)
不使用Boost,只需strptime
即可。
假设此处发布了相同的_adaptive_parser_帮助:Using boost parse datetime string: With single digit hour format
注意:从RFC链接中提取的样本
#include "adaptive_parser.h"
#include <string>
#include <iostream>
int main() {
using namespace mylib::datetime;
adaptive_parser parser;
for (std::string const input : {
"1985-04-12T23:20:50.52Z",
"1996-12-19T16:39:57-08:00",
"1990-12-31T23:59:60Z",
"1990-12-31T15:59:60-08:00",
"1937-01-01T12:00:27.87+00:20",
})
try {
std::cout << "Parsing '" << input << "'\n";
std::cout << " -> epoch " << parser(input).count() << "\n";
} catch(std::exception const& e) {
std::cout << "Exception: " << e.what() << "\n";
}
}
打印
Parsing '1985-04-12T23:20:50.52Z'
-> epoch 482196050
Parsing '1996-12-19T16:39:57-08:00'
-> epoch 851042397
Parsing '1990-12-31T23:59:60Z'
-> epoch 662688000
Parsing '1990-12-31T15:59:60-08:00'
-> epoch 662688000
Parsing '1937-01-01T12:00:27.87+00:20'
-> epoch -1041335973
当然,您可以限制所需子集的已接受模式数。
答案 1 :(得分:0)
有解析时区的限制。
#include <sstream>
#include <iostream>
#include <string>
#include <iomanip>
int main() {
std::tm t = {};
std::string s = "2016-01-02T15:04:05+09:00";
int tz_offset = 0;
auto pos = s.find_last_of("+-Z");
if (pos != s.npos) {
std::string zone = s.substr(pos);
tz_offset = std::atoi(zone.c_str());
s.resize(pos);
}
std::stringstream ss(s);
ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S");
if (ss.fail()) {
std::cout << "Parse failed\n";
} else {
std::time_t l = std::mktime(&t);
std::tm tm_utc(*std::gmtime(&l));
std::time_t utc = std::mktime(&tm_utc);
tz_offset += (utc - l);
l = std::mktime(&t) - tz_offset;
t = *std::gmtime(&l);
std::cout << std::put_time(&t, "%c") << std::endl;
}
}