gpt4 book ai didi

c++ - 用 ColeDateTimeSpan 将天数减去 ColeDateTime

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:54:35 25 4
gpt4 key购买 nike

我必须为我的一项作业更新一个旧的日期类,我卡在这个功能上,我必须重做。

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

谢谢!

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

最佳答案

我知道你的任务太晚了,但是,其他人可能会发现它有用。

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;
}

关于c++ - 用 ColeDateTimeSpan 将天数减去 ColeDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21912439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com