c++ - 将日期 std::string 转换为 QuantLib::Date 对象

标签 c++ date quantlib

由于经常碰巧从 .csv 或 .txt 文件中读取此类字符串,我想知道获取 %d/%m%/%y(或任何其他类似格式)字符串并将其转换为适合 QuantLib::Date 对象的内容 constructor .

下面是示例代码:

#include <ql/quantlib.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>
#include <boost/algorithm/string.hpp>

int main() {

  boost::timer timer;
  std::cout << std::endl;
  std::string datesString = {
    ",17/10/2014,21/11/2014,19/12/2014,20/03/2015,19/06/2015,18/09/2015,18/12/2015,17/06/2016,"
  };
  std::vector<std::string> expiryDates;
  boost::split(expiryDates, datesString, boost::is_any_of(","));
  for(int i = 0; i < expiryDates.size(); i++)
  {
    std::cout << expiryDates[i] << std::endl;
  }
  // 17/10/2014
  // 21/11/2014
  // 19/12/2014
  // 20/03/2015
  // 19/06/2015
  // 18/09/2015
  // 18/12/2015
  // 17/06/2016

  // QuantLib::Date myQLDate(?);

  return 0;

  }

最佳答案

有点隐藏,但是一旦包含 <ql/utilities/dataparsers.hpp>你可以使用:

Date d = DateParser::parseFormatted(expiryDates[i], format);

哪里format是一个 Boost.Date 格式字符串。在你的情况下,

Date d = DateParser::parseFormatted(expiryDates[i], "%d/%m/%Y");

应该可以解决问题。

关于c++ - 将日期 std::string 转换为 QuantLib::Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301337/

相关文章:

c++ - 在 C++ 中可视化 3D 条形图

java - 如何像UAC窗口一样控制键盘和鼠标

java - android 中的 Calendar 类有什么问题吗?

c++ - 在 C++ 中乘以枚举

C++ Opencv - 通过套接字获取图像后的图像处理

c++ - 将 C 类型字符串数组中的元素作为 BSTR 传递给 COM 对象? (在 C++ 中)

javascript - 如何使用 "+"运算符连接两个字符串?

php - 比较 php 中的 mysql 日期

python - 构建 Python 时出现 QuantLib 1.9 fatal error

r - 无法让 RQuantLib 在 osx 10.9.4 下与brew安装的 quantlib 一起使用