来自std :: string的std :: chrono :: time_point

问题描述 投票:1回答:2

我试图将日期(以std::string的形式)转换为std::chrono::time_point。为此,我使用了Boost Date Time。下面,您可以找到一个最小的工作示例。但是,我不明白为什么某些在我看来无效的输入字符串似乎不会以某种方式引起异常。我无法弄清楚这里发生了什么。

#include <iostream>
#include <chrono>
#include <sstream>
#include <boost/date_time.hpp>

using Clock = std::chrono::system_clock;
using TimePoint = std::chrono::time_point<Clock>;

TimePoint timePointFromString(const std::string& date, const std::string& format) {
  // local takes care of destructing time_input_facet
  auto loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));

  std::stringstream ss{date};
  ss.imbue(loc);

  boost::posix_time::ptime pt;
  ss >> pt;

  if (!ss.good()) {
    throw std::runtime_error("Cannot parse string");
  }

  boost::posix_time::ptime time_t_epoch{boost::gregorian::date(1970, 1, 1)};
  boost::posix_time::time_duration diff = pt - time_t_epoch;

  Clock::duration duration{diff.total_nanoseconds()};

  return TimePoint{duration};
}

int main() {
  std::string format{"%Y-%m-%d"};

  std::vector<std::string> strings {"2018", "2018-", "19700101", "19700103", "19700301"};
  for (const auto& s: strings) {
    auto tp = timePointFromString(s, format);
    std::cout << s << ": " << TimePoint::clock::to_time_t(tp) << std::endl;
  }
}

输出:

2018: 1514764800
2018-: 1514764800
19700101: 23587200
19700103: 23587200
terminate called after throwing an instance of 'std::runtime_error'
  what():  Cannot parse string

更新:我认为它会做某种模式匹配,从而误解了这段代码。情况并非如此(请参阅ÖöTiib的回答和他的回答下面的评论)!显然,最好使用Howard Hinnant的date/time库。

c++ boost chrono
2个回答
1
投票

以下是使用Howard Hinnant's free, open-source date/time library时代码的外观:

#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::chrono::system_clock::time_point
timePointFromString(const std::string& date, const std::string& format)
{
    std::stringstream ss{date};
    std::chrono::system_clock::time_point pt;
    ss >> date::parse(format, pt);
    if (ss.fail())
        throw std::runtime_error("Cannot parse date");
    return pt;
}

int
main()
{
    std::string format{"%Y-%m-%d"};
    std::vector<std::string> strings{"2018", "2018-", "19700101", "19700103", "19700301",
                                     "1970-03-01"};
    for (const auto& s: strings)
    {
        try
        {
            auto tp = timePointFromString(s, format);
            using date::operator<<;
            std::cout << s << ": " << tp << '\n';
        }
        catch (std::exception const& e)
        {
            std::cout << s << ": " << e.what() << '\n';
        }
    }
}

输出将是:

2018: Cannot parse date
2018-: Cannot parse date
19700101: Cannot parse date
19700103: Cannot parse date
19700301: Cannot parse date
1970-03-01: 1970-03-01 00:00:00.000000

我在向量的末尾添加了一个有效的字符串/日期,以显示它使用此format接受的内容。 1970-03-01 00:00:00.0...上的尾随零的数量将根据平台的std::chrono::system_clock::time_point的精度而变化。

此代码也应该很容易移植到C ++ 20:

  • 放下#include "date/date.h"
  • 放下using date::operator<<;
  • date::parse改为std::chrono::parse

更新

为了帮助您解释结果:

  • 1970-01-01 00:00:00之后的1514764800s是2018-01-01 00:00:00
  • 1970-01-01 00:00:00之后的23587200s是1970-10-01 00:00:00

(都忽略了闰秒这是常态)


1
投票

你没有解释你的期望,为什么我只是描述你的程序的作用。

使用“19700101”和“19700103”它解析“1970”跳过一个字符解析“10”跳过一个字符并找到字符串的结尾,因此它得出结论,两者都是1970年10月。

使用“19700301”它解析“1970”跳过一个字符解析“30”并从第30个月开始抛出是无稽之谈。

你的输出描述也有拼写错误,你抛出“无法解析日期”而不是“无法解析字符串”。

© www.soinside.com 2019 - 2024. All rights reserved.