2

我必须为我的一项任务更新一个旧的日期类,我被困在这个功能上,我必须重做。

如果可以进行操作,该函数需要返回一个 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;
    }   
}

谢谢!

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

4

1 回答 1

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;
}
于 2018-12-07T01:12:50.870 回答