|
|
Title |
CTime constructor
|
Summary |
Method to call the CTime constructor that will always succeed event with invalid time values |
Contributor |
John McTainsh
|
Published |
27-Jan-2003 |
Last updated |
27-Jan-2003 |
|
|
Description.
Here are some handy methods to prevent the annoying asserts that are thrown
by the CTime class when values are out of range. There are two
methods, one creates the closest time possible to the given time. The other
returns a zero time value if any constructor input is out of range.
///////////////////////////////////////////////////////////////////////////////
// DESC:
// Limit the value to be equal to or within the given value
// CREATED:
// 25 Jan 2003 - John McTainsh
void Limit( int* pnValue, int nLower, int nUpper )
{
if( *pnValue < nLower )
*pnValue = nLower;
if( *pnValue > nUpper )
*pnValue = nUpper;
}
///////////////////////////////////////////////////////////////////////////////
// DESC:
// Return a time value that will be guarantee to be valid. However if the
// time is not within suitable limits it will be modified to fit. This
// result in a different date than was entered
// CREATED:
// 25 Jan 2003 - John McTainsh
CTime MakeCloseTime(
int nYear, int nMonth, int nDay,
int nHour, int nMin, int nSec,
int nDST = -1 )
{
Limit( &nYear, 1971, 2999 );
Limit( &nMonth, 1, 12 );
Limit( &nDay, 1, 31 );
Limit( &nHour, 0, 23 ); // May want to skip these as they
Limit( &nMin, 0, 59 ); // are valid in some circumstances
Limit( &nSec, 0, 59 ); // ..
return CTime( nYear, nMonth, nDay, nHour, nMin, nSec, nDST );
}
The following is useful when you don't want find out if the time value is useable.
///////////////////////////////////////////////////////////////////////////////
// DESC:
// Return a test indicating if the value is within or equal to the limits
// CREATED:
// 25 Jan 2003 - John McTainsh
#define IS_BETWEEN( lower, test, upper ) ( ( lower <= test ) && ( test <= upper ) )
///////////////////////////////////////////////////////////////////////////////
// DESC:
// Make a valid time value if one can be created. If not then return 0
// which can be checked for using CTime::GetTime();
// RETURN:
// A valid time value or 0 if outside range.
// CREATED:
// 25 Jan 2003 - John McTainsh
CTime MakeSafeTime(
int nYear, int nMonth, int nDay,
int nHour, int nMin, int nSec,
int nDST = -1 )
{
if( IS_BETWEEN( 1971, nYear, 2999 ) &&
IS_BETWEEN( 1, nMonth, 12 ) &&
IS_BETWEEN( 1, nDay, 31 ) &&
IS_BETWEEN( 0, nHour, 59 ) && // May want to skip these as they
IS_BETWEEN( 0, nMin, 59 ) && // are valid in some circumstances
IS_BETWEEN( 0, nSec, 59 ) ) // ..
return CTime( nYear, nMonth, nDay, nHour, nMin, nSec, nDST );
return CTime( 0 );
}
|