Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the number of days in a month

Tags:

c

I am receiving the year, month and day as inputs and I am trying to validate the inputs in an efficient way. The year range is [0-99] (0,4,8.. are considered leap years), month range [1-12] and day range [1-31].

The straight forward way of validating the day would be the following:

if( (Day<1u) || (Day>31u) ){
    /*error*/
}
else if ( (Month==4u) || (Month==6u) || (Month==9u) || (Month==11u) && (Day>30u) ){
    /*error*/
}
else if ( (Month==2u) && (Year % 4u == 0u) && (Day > 29u) ){
    /*error*/
}
else if ( (Month==2u) && (Year % 4u != 0u) && (Day > 28u) ){
    /*error*/
}
else
{
    /*valid*/
}

But it has a high complexity.

A lookup table seems like a better choice. And now the question:

Is there a more efficient way of creating the table for this case other than the following?

const int testTable[4][12] = {
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

if( testTable[ Year % 4 ][ Month - 1 ] >= Day){
    /*valid*/
}
else{
    /*error*/
}

Is there another rule that I'm not seeing?

like image 641
Alexandru Cimpanu Avatar asked Sep 13 '16 06:09

Alexandru Cimpanu


People also ask

How do I calculate days in a month in Excel?

Explanation. The DAY function returns the day component of a date. The EOMONTH function returns the last day of the month for a given date. So, in this formula EOMONTH first returns a date corresponding to the last day of the month, and then DAY returns the date value for that date.

How do you calculate number of days?

How do I go about calculating the days between two dates? To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months.

How do you calculate the number of days in a month between two dates?

Go to the first day of the next month, and subtract one day. That gives you the total number of days for the current month. Make sure the answer is formatted as a number, not a date.


1 Answers

You need one dimension for leap years and another one for non leap years:

int isleap(int year)
{
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int mthdays(int month, int year)
{
    static const int days[2][13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    int leap = isleap(year);

    return days[leap][month];
}

The year range is [0-99] (0,4,8.. are considered leap years)

Then your isleap() function must be:

int isleap(int year)
{
    return (year % 4 == 0);
}

month range [1-12]

Using:

{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

instead of

{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

you can avoid [ Month - 1 ]

like image 74
David Ranieri Avatar answered Oct 23 '22 22:10

David Ranieri