It is good habits to check the expiry date of any products like cosmetic products, milk products, etc. Such products can no longer meet safety requirements as soon as the expiry date has been passed.
Sometimes in C application, we need to implement the logic to validate the expiry date of the product. So In this article, I am explaining how you can implement the logic to validate the expiry date of the product to enter their expiry date.
The below program asks the expiry date in the format of years, months, and days. When the user enters the date in the format of years, months and days then it validates the expiry date on the basis of the current date.
Some products have a barcode for the expiry date in that situation there should be a barcode scanner that reads the expiry date from the products and send the expiry date to the application to validate the expiry date. So let see a simple logic to verify the expiry date of the product.
I am following the below steps to accomplish the program,
- Enters years, months, and days.
- Verify the validity of the expiry date.
- Get current time from the system.
- Now check the expiry date.
If you want to learn more about the c language, here 10 Free days C video course for you.
See below program to check the expiry date,
#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,¤tDate); 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
Code Analysis:
In the above c code example, first, we ask the users to enter the expiry date of the product. Using the if condition I am verifying the validity of the entered date.
status = enterExpiryDate(&expiryDate); if(status !=1) { printf("\n\n Please enter a valid date!\n"); return 0; }
Now get the time using the time function. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. So we have to calculate the current time.
//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;
Now expiry date checker C program asks the user to hit 5 from the keyboard to validate the expiry date of the product. If the user enters 5, the code checks the expiry date and displays the result on the console.
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,¤tDate); if(status !=0) { printf("\n\n Product date has been expired !!!\n"); } else { printf("\n\n You can use !!!\n"); }
Recommended Articles for you:
- How to check leap year using C?
- Check date validity in C?
- Find the prime number using the C program.
- find all prime numbers up to n using trial division and Sieve of Eratosthenes algorithm.
- How to use if in C programming.
- How to use C if-else condition?
- Create an employee record system in C.
- Way to create a library management system in C.
- How to create student record system in C?
- How to use for loop in C?
- You should know while loop use.
- When we should use do while in the C program.
- Use of the switch case in the C program.
- C language character set.
- Elements of C Language.
- Data type in C language.
- Operators with Precedence and Associativity.
- How to pass an array as a parameter?
- Memory Layout in C.
- File handling in C, In a few hours.
- Replacing nested switches with the multi-dimensional array
- How to access a two-dimensional array using pointers?
- Brief Introduction of switch case in C.
- 100 C interview Questions.
- Function pointer in c, a detailed guide.
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- Pointer Arithmetic in C.
- Brief Introduction of void pointer in C.