Monday 15 August 2011

validation - How to check if a date exists C -



validation - How to check if a date exists C -

if have next user input: year, month , day. how check if date ever existed?

for example: 2014-10-32 (false) or 2014-09-31 (false) or 2009-02-28 (true)

mktime() in c api returns -1 if the calendar time cannot represented. next code snippet can help check same (given day nday, month nmonth , year nyear arguments):

tm tmdate; memset( &tmdate, 0, sizeof(tm) ); tmdate.tm_mday = nday; tmdate.tm_mon = (nmonth - 1); tmdate.tm_year = (nyear - 1900); time_t tcal= mktime( &tmdate); if( tcal == (time_t) -1 ) homecoming false;

the above snippet illustrates idea. more robust code can found at: http://www.codeproject.com/articles/4722/how-to-check-if-a-datetime-exists

c validation date

No comments:

Post a Comment