在 Boost 中解析 RFC3339/ISO 8601 时间戳



如何解析 C++03 中的RFC3339时间戳("1985-04-12T23:20:50.52Z"((即 ISO8601 的子集(? 我正在使用 Boost,但似乎没有一个 Boost 日期时间库包含执行此操作的函数。

实际时间对象的类型无关紧要,只要我可以轻松地将其与"现在"进行比较即可。 我只关心 UTC 时区的时间戳。

解析时区有限制。

#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 failedn";
  } 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;
  }
}

不使用 Boost,只需strptime就可以。

假设此处发布的相同_adaptive_parser_助手: 使用提升解析日期时间字符串:采用个位数小时格式

注意:从 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

当然,您可以限制所需子集的接受模式数量。

最新更新