使用ColeDateTimeSpan将天数减去ColeDateTime

时间:2014-02-20 15:36:47

标签: c++ coledatetime

我必须为我的任务之一更新一个旧的日期类,我仍然坚持这个函数,我必须重做。

如果可以进行操作,该函数需要返回Bool。

我想做的是将ColeDateTimeSpan的天数减去ColeDateTime

我知道我可以这样做:

int i = 2;    
COleDateTime time_DT = COleDateTime(2014, 2, 20, 0, 0, 0);
COleDateTimeSpan time_SP = COleDateTimeSpan(i);
time_DT = time_DT - time_SP;
cout << time_DT.GetDay() << endl;

在这种情况下,我的函数将返回true;

long i = 999999999999;    
COleDateTime time_DT = COleDateTime(2014, 2, 20, 0, 0, 0);
COleDateTimeSpan time_SP = COleDateTimeSpan(i);
time_DT = time_DT - time_SP;
cout << time_DT.GetDay() << endl;

在这种情况下,我的函数将返回false而不是崩溃

这是我到目前为止所做的:

bool Date::addDays(long days)
{
    bool bRet = true;
    COleDateTimeSpan ts(m_time); //m_time being my COleDateTime
    COleDateTimeSpan tl(days);

    if (tl > ts)
    {
        bRet = false;
        return bRet;
    }
    else
    {
        return bRet;
    }   
}

谢谢!

编辑:我的意思是减去......

1 个答案:

答案 0 :(得分:0)

我知道您的任务为时已晚,但是,其他人可能会觉得有用。

bool Date::AddDays(long days)
{
    // Copy the original value to temporary variable so that it is not lost when subtraction results 
    // in invalid time.
    COleDateTime dtTime(m_time);

    // Check the time for validity before performing subtraction.
    if(dtTime.GetStatus() != COleDateTime::valid )
        return false;

    // Check the input for valid value range. Must be positive value.
    if(days < 0)
        return false;

    // Use that constructor of 'COleDateTimeSpan' which takes days as input.
    COleDateTimeSpan tsDays(days, 0,0,0);   // (Days, Hours, Min, Sec).

    // Perform the subtraction.
    dtTime = dtTime - tsDays;

    // Check if the subtraction has resulted into valid time.
    if(dtTime.GetStatus() != COleDateTime::valid )
        return false;

    // Copy the result from temporary variable to class member.
    m_time = dtTime;

    return true;
}