2016-09-24 34 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