Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "date.h"
- #include <time.h>
- void scan_date(date* ret_date)
- {
- do
- {
- printf("Enter the day:");
- scanf("%d",&ret_date->day);
- printf("Enter the month:");
- scanf("%d",&ret_date->month);
- printf("Enter the year:");
- scanf("%d",&ret_date->year);
- }
- while(!validate_date(ret_date));
- }
- date* createFutureData(int day,int month,int year)
- {
- date* gdate = getCurrentDate();
- date* ptr = (date*)malloc(sizeof(date));
- ptr->day = getCurrentDate()->day+day;
- ptr->month = getCurrentDate()->month+month;
- ptr->day = getCurrentDate()->year+year;
- return ptr;
- }
- date* getCurrentDate()
- {
- time_t t = time(NULL);
- struct tm tm = *localtime(&t);
- date* ptr = (date*)malloc(sizeof(date));
- ptr->day =tm.tm_mday;
- ptr->month = tm.tm_mon+1;
- ptr->year = tm.tm_year+1900;
- return ptr;
- }
- /*
- * return 1 if d1> d2
- * return -1 if d2 > d1
- * return 0 if d1 = d2
- */
- int cmpdates(date* d1,date* d2)
- {
- if(d1->year > d2->year) return 1;
- if(d1->year < d2->year) return -1;
- if(d1->year == d2->year)
- {
- if(d1->month > d2->month) return 1;
- if(d1->month < d2->month) return -1;
- if(d1->month==d2->month)
- {
- if(d1->day > d2->day) return 1;
- if(d1->day < d2->day) return -1;
- if(d1->day == d2->day) return 0;
- }
- }
- }
- void printDate(date* myDate)
- {
- printf("\nDate year:%d - month:%d - day:%d\n",myDate->year,myDate->month,myDate->day);
- }
- /*
- Gets the string and splits it using the strtok method..
- Example 12-3-2010 =>day:12 month:3 year:2010
- and validates the date.. and returns current date if the date isnt valid.
- */
- date* dateFromString(char* str2)
- {
- date* ptr = malloc(sizeof(date));
- char *token;
- char str[strlen(str2)+1];
- strcpy(str,str2);
- token = strtok(str, "-");
- ptr->day = atoi(token);
- token = strtok(NULL, "-");
- ptr->month = atoi(token);
- token = strtok(NULL, "-");
- ptr->year = atoi(token);
- if(!validate_date(ptr))
- {
- printf("Invalid date loaded.. returned current date\n");
- return getCurrentDate();
- }
- return ptr;
- }
- int validate_date(date* d)
- {
- if(d->day >= 31 || d->day <1) return 0;
- if(d->month >= 12 || d->month <1) return 0;
- if(d->year >= 2200 || d->year <1999) return 0;
- return 1;
- }
- void copy_date(date* dest,date* src)
- {
- *dest = *src;
- }
- char* dateToString(date* myDate)
- {
- char *date = malloc(12);
- strcat(date,myDate->day);
- strcat(date,"-");
- strcat(date,myDate->month);
- strcat(date,"-");
- strcat(date,myDate->year);
- return date;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement