Q:

Check expiry date using c language

belongs to collection: Calendar Programs in C

0

Check expiry date using c language

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <time.h>
#define MAX_YR  9999
#define MIN_YR  1900
//structure to store date
typedef struct
{
    int yyyy;
    int mm;
    int dd;
} Date;
// Function to check leap year.
//Function returns 1 if leap year
int  IsLeapYear(int year)
{
    return (((year % 4 == 0) &&
             (year % 100 != 0)) ||
            (year % 400 == 0));
}
// returns 1 if given date is valid.
int isValidDate(Date *validDate)
{
    //check range of year,month and day
    if (validDate->yyyy > MAX_YR ||
            validDate->yyyy < MIN_YR)
        return 0;
    if (validDate->mm < 1 || validDate->mm > 12)
        return 0;
    if (validDate->dd < 1 || validDate->dd > 31)
        return 0;
    //Handle feb days in leap year
    if (validDate->mm == 2)
    {
        if (IsLeapYear(validDate->yyyy))
            return (validDate->dd <= 29);
        else
            return (validDate->dd <= 28);
    }
//handle months which has only 30 days
    if (validDate->mm == 4 || validDate->mm == 6 ||
            validDate->mm == 9 || validDate->mm == 11)
        return (validDate->dd <= 30);
    return 1;
}
//return 1 if successfully enter the expiry date
int enterExpiryDate(Date *getDate)
{
    printf("\n Enter year = ");
    scanf("%d",&getDate->yyyy);
    printf("\n\n Enter month = ");
    scanf("%d",&getDate->mm);
    printf("\n\n Enter day = ");
    scanf("%d",&getDate->dd);
    return isValidDate(getDate);
}
//function to validate product expiry date
int checkExpiryDate(const Date* expiryDate, const Date * currentDate)
{
    if (NULL==expiryDate||NULL==currentDate)
    {
        return 0;
    }
    else
    {
        if (expiryDate->yyyy > currentDate->yyyy)
        {
            return 0;
        }
        else if (expiryDate->yyyy < currentDate->yyyy)
        {
            return 1;
        }
        else
        {
            if (expiryDate->mm > currentDate->mm)
            {
                return 0;
            }
            else if (expiryDate->mm < currentDate->mm)
            {
                return 1;
            }
            else
            {
                return (expiryDate->dd >= currentDate->dd)? 0 : 1;
            }
        }
    }
}
int main(void)
{
    time_t rawtime;
    struct tm * timeinfo;
    //variable to store expiry date
    Date expiryDate = {0};
    //variable to store expiry date
    Date currentDate = {0};
    int status = 0;
    int button = 0;
    printf("\n\n Please enter product expiry date!\n");
    status = enterExpiryDate(&expiryDate);
    if(status !=1)
    {
        printf("\n\n Please enter a valid date!\n");
        return 0;
    }
    //Get current time
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    //compose current date
    // years since 1900
    currentDate.yyyy = timeinfo->tm_year+1900;
    // months since January - [0, 11]
    currentDate.mm = timeinfo->tm_mon+1;
    // day of the month - [1,28 or 29 or 30 or 31]
    currentDate.dd = timeinfo->tm_mday;
    printf("\n\n Enter 5 to check product expiry date  = ");
    scanf("%d",&button);
    if((button != 5))
    {
        printf("\n\n You have prssed invalid button !!!\n\n");
        return 0;
    }
    //check expiry date
    status = checkExpiryDate(&expiryDate,&currentDate);
    if(status !=0)
    {
        printf("\n\n Product date has been expired !!!\n");
    }
    else
    {
        printf("\n\n You can use !!!\n");
    }
    return 0;
}

Output:

Current date: 7/9/2018

 Please enter product expiry date!

 Enter year = 2018

 Enter month = 5

 Enter day = 12

 Enter 5 to check product expiry date  = 5

 

 Product date has been expired !!!

---------------------------------------------------------

 Please enter product expiry date!

 Enter year = 2018

 Enter month = 2

 Enter day = 29

 

 Please enter a valid date!

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find century for a year... >>
<< C program to check valid date (date is valid or no...