#include <stdio.h> #include <stdlib.h> #include "datetime.h" #include "datastructure.h" /********************************************************************************************************************/ /* Check if the year is a leap one or not if the out put is diffrent from 0 that means that the year is a leap year */ /********************************************************************************************************************/ int isLeapYear (int year) { if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) return -1; else return 0; } /******************************************************************************************/ /* Check if the Year is Valid : in our case the input year should be between 1000 and 9999*/ /******************************************************************************************/ int CheckYear (int year) { if (year >= 1000 && year <= 9999) return -1; else return 0; } /*************************************************************************************************************/ /* Check if the Month is Valid : The Input month should be between 1-12 referenring to the months of the year*/ /*************************************************************************************************************/ int CheckMonth (int month) { if(month >= 1 && month <= 12) return -1; else return 0; } /*************************************************************************************************************/ /* Check if the day is Valid : The Input day should be between 1-12 referenring to the months of the year*****/ /*************************************************************************************************************/ int Checkday (int day ,int month, int year) { if((day >= 1 && day <= 31) && (month == 1|| month == 3||month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) return -1; else if((day >= 1 && day <= 30) && (month == 4||month == 6||month == 9|| month == 11)) return -1; else if((day >= 1 && day <= 28) && (month == 2)) return -1; else if(day == 29 && month == 2 && (isLeapYear(year))) return -1; else return 0; } /**********************************************************************************************************************/ /* Check if the date is Valid : this functions uses the three functions bellow to check if the whole date is valid*****/ /**********************************************************************************************************************/ int isDateValid(sDate date) { if(CheckYear(date.year)) { if(CheckMonth(date.month)) return Checkday(date.day, date.month, date.year); else return 0 ; } else return 0 ; } /**********************************************************************************************************************/ /* Get the day from Input: this functions gets the day from a string Input and convert it into integer*****************/ /**********************************************************************************************************************/ int getDay (char* day) { return atoi(day); } /**************************************************************************************************************************/ /* Get the month from Input: this functions gets the month from a string Input and convert it into integer*****************/ /**************************************************************************************************************************/ int getMonth(char* month) { unsigned int cursor = 0; do { cursor ++; } while ((*(month + cursor) != '.') && (*(month + cursor) != '\0')); return atoi((month + cursor + 1)); } /**************************************************************************************************************************/ /* Get the year from Input: this functions gets the year from a string Input and convert it into integer*******************/ /**************************************************************************************************************************/ int getYear(char* Year) { unsigned int brake_points = 0; unsigned int cursor = 0; do { cursor ++ ; if (*(Year + cursor) == '.') brake_points ++; } while (brake_points < 2); return atoi (Year + cursor +1); } /*******************************************************************************************************************************************/ /* Check the validity of the date: this functions gets the Input as a string and checks how many break point in the Input*******************/ /*******************************************************************************************************************************************/ int brake_points_check (char* date) { unsigned int brake_points = 0; unsigned int cursor = 0; do { cursor ++ ; if (*(date + cursor) == '.') brake_points ++; } while ( (*(date + cursor) != '\0')); return brake_points > 2 ? 0 : -1; } /*******************************************************************************************************************************************/ /* Convert string input into sDate data structure ******************************************************************************************/ /*******************************************************************************************************************************************/ int getDateFromString (char* input ,sDate *date) { date->day = getDay(input); date->month = getMonth(input); date->year = getYear(input); if ((isDateValid(*date)) && (brake_points_check(input))) return -1; else return 0; }