示例#1
0
RcppExport SEXP businessDaysBetween(SEXP calSexp, SEXP params,
                                       SEXP from, SEXP to){

    try {
        boost::shared_ptr<QuantLib::Calendar> pcal( getCalendar(Rcpp::as<std::string>(calSexp)) );
        Rcpp::List rparam(params);
        double ifirst = Rcpp::as<double>(rparam["includeFirst"]);
        double ilast = Rcpp::as<double>(rparam["includeLast"]);

        Rcpp::DateVector dates1  = Rcpp::DateVector(from);
        Rcpp::DateVector dates2  = Rcpp::DateVector(to);

        int n = dates1.size();
        std::vector<double> between(n);

        for (int i=0; i<n; i++) {
            QuantLib::Date day1( dateFromR(dates1[i]) );
            QuantLib::Date day2( dateFromR(dates2[i]) );
            between[i] = pcal->businessDaysBetween(day1, day2,
                                                   (ifirst == 1) ? true: false,
                                                   (ilast == 1) ? true: false);
        }
        
        return Rcpp::wrap(between);

    } catch(std::exception &ex) { 
        forward_exception_to_r(ex); 
    } catch(...) { 
        ::Rf_error("c++ exception (unknown reason)"); 
    }

    return R_NilValue;
}
void CBCGPPlannerPrintMonth::OnDrawWeekBar (CDC* pDC, const CRect& rectBar)
{
	if (rectBar.IsRectEmpty ())
	{
		return;
	}

	ASSERT_VALID (pDC);

	const int nRows = GetViewDuration () / 7;

	const BOOL bDateBeforeMonth = CBCGPPlannerView::IsDateBeforeMonth ();

	CStringArray saFormat;
	{
		CString strMonthFmt (_T("MMM"));
		CString strSep (_T(" "));

		CFont* pOldFont = pDC->SelectObject (&m_Font);

		CSize szText (pDC->GetTextExtent (_T("AAA 00 - AAA 00")));

		CRect rect (m_ViewRects[0]);
		rect.right = rectBar.right;
		rect.left  = rectBar.left - m_OnePoint.cy;
		rect.top   += m_nRowHeight + m_OnePoint.cy;
		if (rect.Height() < (szText.cx * 1.5))
		{
			strMonthFmt = _T("M");
			strSep      = CBCGPPlannerView::GetDateSeparator ();
		}

		pDC->SelectObject (pOldFont);

		CString strFormat;
		if (bDateBeforeMonth)
		{
			strFormat = _T("d") + strSep + strMonthFmt;
			saFormat.Add (_T("d"));
			saFormat.Add (strFormat);
			saFormat.Add (strFormat);
		}
		else
		{
			strFormat = strMonthFmt + strSep + _T("d");
			saFormat.Add (strFormat);
			saFormat.Add (_T("d"));
			saFormat.Add (strFormat);
		}
	}

	COLORREF clrTextOld = pDC->SetTextColor (m_clrText);
	CFont* pOldFont = pDC->SelectObject (&m_FontVert);
	CPen* pOldPen = pDC->SelectObject (&m_penBlack);

	COleDateTime day1 (GetDateStart ());
	COleDateTime day2 (day1);
	day2 += COleDateTimeSpan (6, 0, 0, 0);

	COleDateTimeSpan span (7, 0, 0, 0);

	int nIndex = 0;

	if (IsCompressWeekend () && m_ViewRects[0].top != m_ViewRects[1].top)
	{
		nIndex = 2;
	}

	for (int iRow = 0; iRow < nRows; iRow++)
	{
		CRect rect (m_ViewRects[nIndex]);
		rect.right = rectBar.right;
		rect.left  = rectBar.left - m_OnePoint.cy;
		rect.top    += m_nRowHeight + m_OnePoint.cy;

		if (rect.top < rect.bottom)
		{
			pDC->FillRect (rect, &m_brGray);
			pDC->MoveTo (rect.right, rect.top);
			pDC->LineTo (rect.left, rect.top);
			pDC->LineTo (rect.left, rect.bottom);
			pDC->LineTo (rect.right, rect.bottom);

			rect.DeflateRect (m_OnePoint.cx, m_OnePoint.cy);

			SYSTEMTIME st1;
			day1.GetAsSystemTime (st1);
			SYSTEMTIME st2;
			day2.GetAsSystemTime (st2);

			CString strDate1;
			CString strDate2;

			if (day1.GetMonth () == day2.GetMonth ())
			{
				strDate1.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st1, 
					saFormat[0], (LPTSTR)(LPCTSTR)strDate1, _MAX_PATH);
				strDate1.ReleaseBuffer ();

				strDate2.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st2, 
					saFormat[1], (LPTSTR)(LPCTSTR)strDate2, _MAX_PATH);
				strDate2.ReleaseBuffer ();
			}
			else
			{
				strDate1.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st1, 
					saFormat[2], (LPTSTR)(LPCTSTR)strDate1, _MAX_PATH);
				strDate1.ReleaseBuffer ();

				strDate2.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st2, 
					saFormat[2], (LPTSTR)(LPCTSTR)strDate2, _MAX_PATH);
				strDate2.ReleaseBuffer ();
			}

			CString strText = strDate1 + _T(" - ") + strDate2;

			CSize szText (pDC->GetTextExtent (strText));

			CRect rectText (rect);
			rectText.left = rectText.left + (rect.Width () - szText.cy + m_OnePoint.cx) / 2;

			rectText.top = rectText.bottom;
			if (szText.cx < rect.Height ())
			{
				rectText.top -= (rect.Height () - szText.cx + m_OnePoint.cy) / 2;
			}
			rectText.bottom = rect.top;

			pDC->DrawText (strText, rectText, DT_SINGLELINE);
		}

		nIndex += 7;
		day1 += span;
		day2 += span;
	}

	pDC->SelectObject (pOldPen);
	pDC->SelectObject (pOldFont);
	pDC->SetTextColor (clrTextOld);
}
void CBCGPPlannerViewMonth::OnDrawWeekBar (CDC* pDC, const CRect& rectBar)
{
	if (rectBar.IsRectEmpty ())
	{
		return;
	}

	ASSERT_VALID (pDC);

	visualManager->PreparePlannerCaptionBackItem (FALSE);
	visualManager->OnFillPlannerWeekBar (pDC, this, rectBar);

	COLORREF clrLine = visualManager->GetPlannerSeparatorColor (this);

	const int nRows = GetViewDuration () / 7;

	const BOOL bDateBeforeMonth = CBCGPPlannerView::IsDateBeforeMonth ();

	CStringArray saFormat;
	{
		CString strMonthFmt (_T("MMM"));
		CString strSep (_T(" "));

		HFONT hOldFont = (HFONT)::SelectObject (pDC->GetSafeHdc (), GetFont ());

		CSize szText (pDC->GetTextExtent (_T("AAA 00 - AAA 00")));

		CRect rect (m_WeekRects[0]);
		if (rect.Height() < (szText.cx * 1.5))
		{
			strMonthFmt = _T("M");
			strSep      = CBCGPPlannerView::GetDateSeparator ();
		}

		if (hOldFont != NULL)
		{
			::SelectObject (pDC->GetSafeHdc (), hOldFont);
		}

		CString strFormat;
		if (bDateBeforeMonth)
		{
			strFormat = _T("d") + strSep + strMonthFmt;
			saFormat.Add (_T("d"));
			saFormat.Add (strFormat);
			saFormat.Add (strFormat);
		}
		else
		{
			strFormat = strMonthFmt + strSep + _T("d");
			saFormat.Add (strFormat);
			saFormat.Add (_T("d"));
			saFormat.Add (strFormat);
		}
	}

	COLORREF clrTextOld = pDC->GetTextColor ();
	HFONT hOldFont = (HFONT)::SelectObject (pDC->GetSafeHdc (), GetFontVert ());

	COleDateTime day1 (GetDateStart ());
	COleDateTime day2 (day1);
	day2 += COleDateTimeSpan (6, 0, 0, 0);

	COleDateTimeSpan span (7, 0, 0, 0);

	for (int iRow = 0; iRow < nRows; iRow++)
	{
		CRect rect (m_WeekRects[iRow]);

		if (rect.top < rect.bottom)
		{
			COLORREF clrText = 
				visualManager->OnFillPlannerCaption (pDC, this, rect, FALSE, FALSE, TRUE, FALSE);

			pDC->SetTextColor (clrText);

			pDC->Draw3dRect (rect, clrLine, clrLine);

			rect.DeflateRect (1, 1);

			SYSTEMTIME st1;
			day1.GetAsSystemTime (st1);
			SYSTEMTIME st2;
			day2.GetAsSystemTime (st2);

			CString strDate1;
			CString strDate2;

			if (day1.GetMonth () == day2.GetMonth ())
			{
				strDate1.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st1, 
					saFormat[0], (LPTSTR)(LPCTSTR)strDate1, _MAX_PATH);
				strDate1.ReleaseBuffer ();

				strDate2.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st2, 
					saFormat[1], (LPTSTR)(LPCTSTR)strDate2, _MAX_PATH);
				strDate2.ReleaseBuffer ();
			}
			else
			{
				strDate1.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st1, 
					saFormat[2], (LPTSTR)(LPCTSTR)strDate1, _MAX_PATH);
				strDate1.ReleaseBuffer ();

				strDate2.GetBuffer (_MAX_PATH);
				::GetDateFormat (LOCALE_USER_DEFAULT, 0, &st2, 
					saFormat[2], (LPTSTR)(LPCTSTR)strDate2, _MAX_PATH);
				strDate2.ReleaseBuffer ();
			}

			CString strText = strDate1 + _T(" - ") + strDate2;

			CSize szText (pDC->GetTextExtent (strText));

			CRect rectText (rect);
			rectText.left = rectText.left + (rect.Width () - szText.cy + 1) / 2;

			rectText.top = rectText.bottom;
			if (szText.cx < rect.Height ())
			{
				rectText.top -= (rect.Height () - szText.cx + 1) / 2;
			}
			rectText.bottom = rect.top;

			pDC->DrawText (strText, rectText, DT_SINGLELINE);
		}

		day1 += span;
		day2 += span;
	}

	pDC->SetTextColor (clrTextOld);
	if (hOldFont != NULL)
	{
		::SelectObject (pDC->GetSafeHdc (), hOldFont);
	}
}