Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include "date.h"
  2. #include <time.h>
  3.  
  4. void scan_date(date* ret_date)
  5. {
  6. do
  7. {
  8. printf("Enter the day:");
  9. scanf("%d",&ret_date->day);
  10. printf("Enter the month:");
  11. scanf("%d",&ret_date->month);
  12. printf("Enter the year:");
  13. scanf("%d",&ret_date->year);
  14. }
  15. while(!validate_date(ret_date));
  16.  
  17. }
  18.  
  19. date* createFutureData(int day,int month,int year)
  20. {
  21. date* gdate = getCurrentDate();
  22. date* ptr = (date*)malloc(sizeof(date));
  23. ptr->day = getCurrentDate()->day+day;
  24. ptr->month = getCurrentDate()->month+month;
  25. ptr->day = getCurrentDate()->year+year;
  26. return ptr;
  27. }
  28.  
  29. date* getCurrentDate()
  30. {
  31. time_t t = time(NULL);
  32. struct tm tm = *localtime(&t);
  33. date* ptr = (date*)malloc(sizeof(date));
  34. ptr->day =tm.tm_mday;
  35. ptr->month = tm.tm_mon+1;
  36. ptr->year = tm.tm_year+1900;
  37. return ptr;
  38. }
  39. /*
  40. * return 1 if d1> d2
  41. * return -1 if d2 > d1
  42. * return 0 if d1 = d2
  43. */
  44. int cmpdates(date* d1,date* d2)
  45. {
  46. if(d1->year > d2->year) return 1;
  47. if(d1->year < d2->year) return -1;
  48. if(d1->year == d2->year)
  49. {
  50. if(d1->month > d2->month) return 1;
  51. if(d1->month < d2->month) return -1;
  52. if(d1->month==d2->month)
  53. {
  54. if(d1->day > d2->day) return 1;
  55. if(d1->day < d2->day) return -1;
  56. if(d1->day == d2->day) return 0;
  57. }
  58. }
  59. }
  60.  
  61. void printDate(date* myDate)
  62. {
  63. printf("\nDate year:%d - month:%d - day:%d\n",myDate->year,myDate->month,myDate->day);
  64. }
  65.  
  66. /*
  67. Gets the string and splits it using the strtok method..
  68. Example 12-3-2010 =>day:12 month:3 year:2010
  69. and validates the date.. and returns current date if the date isnt valid.
  70. */
  71. date* dateFromString(char* str2)
  72. {
  73. date* ptr = malloc(sizeof(date));
  74. char *token;
  75.  
  76. char str[strlen(str2)+1];
  77. strcpy(str,str2);
  78.  
  79. token = strtok(str, "-");
  80. ptr->day = atoi(token);
  81.  
  82. token = strtok(NULL, "-");
  83. ptr->month = atoi(token);
  84.  
  85. token = strtok(NULL, "-");
  86. ptr->year = atoi(token);
  87.  
  88. if(!validate_date(ptr))
  89. {
  90. printf("Invalid date loaded.. returned current date\n");
  91. return getCurrentDate();
  92. }
  93. return ptr;
  94. }
  95.  
  96. int validate_date(date* d)
  97. {
  98. if(d->day >= 31 || d->day <1) return 0;
  99. if(d->month >= 12 || d->month <1) return 0;
  100. if(d->year >= 2200 || d->year <1999) return 0;
  101. return 1;
  102. }
  103. void copy_date(date* dest,date* src)
  104. {
  105. *dest = *src;
  106. }
  107. char* dateToString(date* myDate)
  108. {
  109. char *date = malloc(12);
  110. strcat(date,myDate->day);
  111. strcat(date,"-");
  112. strcat(date,myDate->month);
  113. strcat(date,"-");
  114. strcat(date,myDate->year);
  115. return date;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement