Introduction
How long has it been since your last antivirus scan, e-mail login or other recurring process? The answer is usually displayed as minutes, hours or days. Are these days calendar days or 24-hour days? If the times are available, you can figure that out. After the first 24 hours, I presume that most people would like to see a difference as calendar days. The following solution shows how to calculate calendar or 24-hour days.
Solution
The following function allows for displaying a time difference as minutes, hours or days, where you can choose between calendar days and 24-hour days. In this function and the sample program CalDayDiff
, the MFC
class CTime
is used. Starting with Visual Studio 2005, the upper date limit on CTime
became December 31, 3000. Prior to that version, the upper date limit on CTime
was January 18, 2038. This program also contains source code for using the MFC
class COleDateTime
, which has the upper date limit of December 31, 9999.
enum SpanType {Minutes, Hours, Days};
int CCalDayDiffDlg::GetDaysDiff (CTime& dtFirst, CTime& dtSecond, bool b24Show ,
bool bCalDays , int* pnType )
{
CTimeSpan dtSpan;
if (b24Show) {
dtSpan = dtSecond - dtFirst;
if (dtSpan.GetDays() == 0) {
int nHours = dtSpan.GetHours();
if (nHours == 0) {
if (pnType != nullptr)
*pnType = Minutes;
return dtSpan.GetMinutes();
}
else {
if (pnType != nullptr)
*pnType = Hours;
return nHours;
}
}
}
if (bCalDays) { CTime dtFirstDate (dtFirst.GetYear(), dtFirst.GetMonth(), dtFirst.GetDay(), 0, 0, 0);
CTime dtSecondDate (dtSecond.GetYear(), dtSecond.GetMonth(), dtSecond.GetDay(), 0, 0, 0);
dtSpan = dtSecondDate - dtFirstDate;
}
else { dtSpan = dtSecond - dtFirst;
}
if (pnType != nullptr)
*pnType = Days;
return static_cast<int> (dtSpan.GetDays());
}
If requested, this function handles the first 24 hours differently by returning the number of minutes or hours between the input date/time objects. After that, if determining calendar days is selected, then CTime
objects are created that have their time components set to zero. From these objects, a time-span object is calculated. Otherwise, this time-span object is calculated from the input time/date objects. From this object, the number of elapsed days is returned.
Sample Program
The program CalDayDiff
can show time differences with various options. First, alter the dates and/or times to produce a time difference. Then choose between calendar days and 24-hour days and whether minutes or hours should be displayed for the first 24 hours. These choices can be changed to see the effects of different choices. The data/time difference will be updated with each change. See the above images for some sample choices.
This program contains the following macro:
#define SINGULAR(n) (abs (n) == 1 ? _T("") : _T("s"))
It is essentially the macro that I wrote about in Singular vs. Plural in Item Counts. The difference is that it includes abs
since n
might be negative. Its purpose is to make it easier to select between singular and plural versions of words. It is first used in the following line of CalDayDiff
:
strMsg.Format (_T("%d minute%s"), nResult, SINGULAR (nResult));