-1

If I have the following user input: year, month and day. How do I check if that date actually ever existed?

For example: 2014-10-32 (FALSE) or 2014-09-31 (FALSE) or 2009-02-28 (TRUE)

Ajay
  • 6,396
  • 17
  • 68
  • 126
Tiago Redaelli
  • 482
  • 4
  • 14
  • possible duplicate of [A C program to check if the entered date is valid or not](http://stackoverflow.com/questions/14950012/a-c-program-to-check-if-the-entered-date-is-valid-or-not) – Ajay Oct 09 '14 at 05:10
  • 1
    @AjayPunekar, the link describes how to check if a user-entered date is syntactically correct. The OP wants to check if a date is a valid one, as in, ever existed on the calendar. – Ankur Kanoria Oct 09 '14 at 05:20
  • A good solution would be first creating a Calender and Check whether the pattern of date exist on that calender or not. – Asis Oct 09 '14 at 05:30

2 Answers2

1

mktime() in the C API returns -1 if the the calendar time cannot be represented. The following code snippet can help you check for the same (given the day nDay, month nMonth and year nYear as 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 ) return false;

The above snippet illustrates the idea. More robust code can be found at: http://www.codeproject.com/Articles/4722/How-to-check-if-a-datetime-exists

Ankur Kanoria
  • 817
  • 8
  • 9
  • I don't think that this is as simple. In fact, `mktime` normalizes the time, and if this is possible returns `0`. So one should check in addition that the values after the call are the same. – Jens Gustedt Oct 09 '14 at 07:08
  • You are right @JensGustedt. One should technically make a copy of the date structure, and compare the original and the one modified by the call to mktime(). The link shared above does that and has a robust code example. – Ankur Kanoria Oct 09 '14 at 11:00
  • you could just compare with the three values that you received as parameters, I think. – Jens Gustedt Oct 09 '14 at 11:23
0

SomeThing like this may Help you:

// This calendar example

#include<stdio.h>

#define TRUE    1
#define FALSE   0

int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
    " ",
    "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
};


int inputyear(void)
{
    int year;

    printf("Please enter a year (example: 1999) : ");
    scanf("%d", &year);
    return year;
}

int determinedaycode(int year)
{
    int daycode;
    int d1, d2, d3;

    d1 = (year - 1.)/ 4.0;
    d2 = (year - 1.)/ 100.;
    d3 = (year - 1.)/ 400.;
    daycode = (year + d1 - d2 + d3) %7;
    return daycode;
}


int determineleapyear(int year)
{
    if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
    {
        days_in_month[2] = 29;
        return TRUE;
    }
    else
    {
        days_in_month[2] = 28;
        return FALSE;
    }
}

void calendar(int year, int daycode)
{
    int month, day;
    for ( month = 1; month <= 12; month++ )
    {
        printf("%s", months[month]);
        printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );

        // Correct the position for the first date
        for ( day = 1; day <= 1 + daycode * 5; day++ )
        {
            printf(" ");
        }

        // Print all the dates for one month
        for ( day = 1; day <= days_in_month[month]; day++ )
        {
            printf("%2d", day );

            // Is day before Sat? Else start next line Sun.
            if ( ( day + daycode ) % 7 > 0 )
                printf("   " );
            else
                printf("\n " );
        }
            // Set position for next month
            daycode = ( daycode + days_in_month[month] ) % 7;
    }
}

int main(void)
{
    int year, daycode, leapyear;

    year = inputyear();
    daycode = determinedaycode(year);
    determineleapyear(year);
    calendar(year, daycode);
    printf("\n");
}

For more Detail CodeInGunit

Asis
  • 655
  • 3
  • 19
  • 1
    You may need to include some code to handle leap year. – SSC Oct 09 '14 at 05:24
  • `if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE) { // It is a leap year and February has 29 days. } else { // It is not a leap year, so February has 28 days. }` Like this ?? Thanks for the suggestion – Asis Oct 09 '14 at 05:27