2016-09-24 35 views
1

我最近在QUANTLIB C++中使用了日历函数来执行下面的操作。日历库C++

不幸的是使用QUANTLIB任何项目时间太长编译。我有兴趣以多种不同格式解释日期字符串(Quantlib使我能够做到这一点),如下所示。我也想找到不同格式之间的不同日期之间的差异等。

我的问题是,是否有另一个C++库,使我能够做所有这些事情(希望能够更快地编译我的项目) ?

以下简单的项目似乎需要永远编译。

我唯一的前提是它编译静态。

#include <iostream> 
#include <ql/quantlib.hpp> 
#include <ql/utilities/dataparsers.hpp> 


using namespace std; 
using namespace QuantLib; 

int main() 
{ 

    Calendar cal = Australia(); 
    const Date dt(21, Aug, 1971); 

    bool itis = false; 

    itis = cal.isBusinessDay(dt); 
    cout << "business day yes? " << itis << endl; 
    cout << "The calendar country is: " << cal.name() << endl; 


    // now convert a string to a date. 
    string mydate = "05/08/2016"; 
    const Date d = DateParser::parseFormatted(mydate,"%d/%m/%Y"); 


    cout << "The year of this date is: " << d.year() << endl; 
    cout << "The month of this date is: " << d.month() << endl; 
    cout << "The day of this date is: " << d.dayOfMonth() << endl; 
    cout << "The date " << mydate << " is a business day yes? " << cal.isBusinessDay(d) << endl; 


} 
+2

你可以试试这个:https://howardhinnant.github.io/date/date.html不知道它是否有你需要的所有东西。 – Rostislav

回答

2

date library是完全记录,开放源代码,以及需要的部分仅标头和编译速度非常快。它需要C++ 11或更高版本,因为它建立在<chrono>上。

你的例子如下所示:

#include "date/date.h" 
#include <iostream> 
#include <sstream> 

using namespace std; 
using namespace date; 

int main() 
{ 
    const auto dt = 21_d/aug/1971; 
    auto wd = weekday{dt}; 

    auto itis = wd != sun && wd != sat; 
    cout << "business day yes? " << itis << endl; 

    // now convert a string to a date. 
    istringstream mydate{"05/08/2016"}; 
    local_days ld; 
    mydate >> parse("%d/%m/%Y", ld); 
    auto d = year_month_day{ld}; 
    wd = weekday{ld}; 

    cout << "The year of this date is: " << d.year() << '\n'; 
    cout << "The month of this date is: " << d.month() << '\n'; 
    cout << "The day of this date is: " << d.day() << '\n'; 
    cout << "The date " << d << " is a business day yes? " << (wd != sun && wd != sat) << '\n'; 
} 

上述程序输出:

business day yes? 0 
The year of this date is: 2016 
The month of this date is: Aug 
The day of this date is: 05 
The date 2016-08-05 is a business day yes? 1 

唯一粗略部分是缺乏的isBusinessDay。但是在这个库中查找星期几非常容易(如上所示)。如果你有一个澳大利亚假期的列表,你可以很容易地使用这个图书馆来建立一个更完整的isBusinessDay。例如:

bool 
isBusinessDay(year_month_day ymd) 
{ 
    sys_days sd = ymd; 
    weekday wd = sd; 
    if (wd == sat || wd == sun) // weekend 
     return false; 
    if (sd == mon[2]/jun/ymd.year()) // Queen's Birthday 
     return false; 
    // ... 
    return true; 
} 
+0

非常感谢。这非常有用。 – domonica

+0

我只是我会补充到这一点。我找到了一种使用日期函数所需的一些Quantlib库的方法。只需包含ql/time/calendar.hpp和ql/time/all.hpp。它工作的一种享受。 – domonica