Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C++

How to Check if a datetime Exists

Rate me:
Please Sign up or sign in to vote.
1.55/5 (13 votes)
7 Aug 2003 65.8K   16   7
An article about checking a datetime

Introduction

In one of my projects, I wanted to check if the given date is correct. I tried to find an appropriate API function, but could not find an appropriate function both in the C++ library, and in Platform SDK. Therefore, I decided to create one on my own.

Using the Code

The code is presented as one simple function. You should set the function parameters. The function returns true or false, depending on whether the date is valid.

C++
///
/// Checks if the datetime exists.
/// nDay - <PARAM name="nDay">The day of month from 1 to 31.</PARAM>
/// nMonth - <PARAM name="nMonth">The month from 1 to 12.</PARAM>
/// nYear - <PARAM name="nYear">The years by Gregorian calendar.
/// </PARAM>
/// Returns true if date is correct, otherwise false.
///
bool IsDateValid( const int nDay, const int nMonth, const int nYear ) const
{
    _ASSERT(nDay > 0);
    _ASSERT(nMonth > 0);
    _ASSERT(nYear > 0);

    // check date existing
    tm tmDate;
    memset( &tmDate, 0, sizeof(tm) );
    tmDate.tm_mday = nDay;
    tmDate.tm_mon = (nMonth - 1);
    tmDate.tm_year = (nYear - 1900);

    // copy the entered date to the validate date structure
    tm tmValidateDate;
    memcpy( &tmValidateDate, &tmDate, sizeof(tm) );

    // If timeptr references a date before midnight, 
    // January 1, 1970, or if the calendar time cannot be 
    // represented, mktime returns –1 cast to type time_t.
    time_t timeCalendar = mktime( &tmValidateDate );
    if( timeCalendar == (time_t) -1 ) 
        return false;

    return (
        (tmDate.tm_mday == tmValidateDate.tm_mday) &&
    (tmDate.tm_mon == tmValidateDate.tm_mon) &&
    (tmDate.tm_year == tmValidateDate.tm_year) &&
    (tmDate.tm_hour == tmValidateDate.tm_hour) &&
    (tmDate.tm_min == tmValidateDate.tm_min) &&
    (tmDate.tm_sec == tmValidateDate.tm_sec) );
}

Points of Interest

After some research, I discovered that the ATL includes COleDateTime class, that has the GetStatus() method used for datetime checking. But after code investigation, I discovered that the COleDateTime simply checks datetime range of values.

History

  • 11.07.2003 - First version of the source code

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Bulgaria Bulgaria
I am a senior developer in Melon Technologies Ltd.

Comments and Discussions

 
GeneralDoes not take care of Daylight savings time Pin
parama_chakra6-Jun-06 16:13
parama_chakra6-Jun-06 16:13 
GeneralSave 3 lines of code Pin
YaronNir9-Aug-03 20:48
YaronNir9-Aug-03 20:48 
GeneralRe: Save 3 lines of code Pin
Anonymous10-Aug-03 4:49
Anonymous10-Aug-03 4:49 
GeneralRe: Save 3 lines of code Pin
YaronNir10-Aug-03 4:51
YaronNir10-Aug-03 4:51 
GeneralRe: Save 3 lines of code Pin
Nikolai Serdiuk10-Aug-03 7:05
Nikolai Serdiuk10-Aug-03 7:05 
GeneralEeep! Pin
Kippesoep8-Aug-03 2:42
Kippesoep8-Aug-03 2:42 
The code you present works and may be quite useful, but you need to put some serious work in correcting the spelling for this article (as well as the layout for the code).
GeneralRe: Eeep! Pin
Nikolai Serdiuk10-Aug-03 7:03
Nikolai Serdiuk10-Aug-03 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.