Advertisement
Guest User

Untitled

a guest
Sep 25th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. "datetime.h"
  2.  
  3. #ifndef DATETIME_H_
  4. #define DATETIME_H_
  5.  
  6. #include <exception>
  7.  
  8. namespace RabQavSystem {
  9.  
  10. typedef char* DateTime;
  11.  
  12. #define DATE_TIME_FORMAT 5
  13.  
  14. class DtIllegalTimeDateException : public std::exception{};
  15.  
  16. int dateTimeToMinutes(DateTime datetime);
  17.  
  18. int dateTimeDifference(DateTime datetime1, DateTime datetime2);
  19. }
  20. #endif /* DATETIME_H_ */
  21.  
  22.  
  23. "datetime.cpp"
  24. #include "datetime.h"
  25. #include <stdlib.h>
  26. #include <assert.h>
  27. #include <string.h>
  28. #include <malloc.h>
  29. #include <exception>
  30.  
  31. using namespace RabQavSystem;
  32.  
  33. const int MIN_DAY = 1;
  34. const int MAX_DAY = 31;
  35. const int INVALID_MONTH = 0;
  36. const int MIN_MONTH = 1;
  37. const int MAX_MONTH = 12;
  38. const int MAX_HOURS = 23;
  39. const int MAX_MINUTES = 59;
  40. const int DAYS_IN_YEAR = 365;
  41. const int MINUTES_IN_HOUR = 60;
  42. const int MINUTES_IN_DAY = 24 * 60;
  43. const int DAYS_IN_MONTH = 31;
  44. const int MINUTES_IN_YEAR = 365 * 24 * 60;
  45.  
  46.  
  47. int dateTimeDifference(DateTime datetime1, DateTime datetime2) {
  48. if (datetime1 == NULL || datetime2 == NULL) {
  49. throw DtIllegalTimeDateException();
  50. return 0;
  51. }
  52. int dateTimeInMinutes1 = static_cast<int>(dateTimeToMinutes(datetime1));
  53. int dateTimeInMinutes2 = static_cast<int>(dateTimeToMinutes(datetime2));
  54. int diffInMinutes = ((dateTimeInMinutes2) - (dateTimeInMinutes1));
  55. if ((diffInMinutes) >= 0) {
  56. return diffInMinutes;
  57. } else {
  58. throw DtIllegalTimeDateException();
  59. return 0;
  60. }
  61. }
  62.  
  63. static bool valiDateFormat(DateTime datetime) {
  64. DateTime tmpStr = static_cast<DateTime>(malloc(strlen(datetime) + 1));
  65. DateTime tmpPtr = tmpStr;
  66. if (tmpStr == NULL) {
  67. throw DtIllegalTimeDateException();
  68. return false;
  69. }
  70. strcpy(tmpStr, datetime);
  71. DateTime strDateTime = strtok(tmpStr, "/");
  72. int strLen[DATE_TIME_FORMAT] = { 0 };
  73. for (int i = 0; i < DATE_TIME_FORMAT; i++) {
  74. strLen[i] = strlen(strDateTime);
  75. switch (i) {
  76. case 0:
  77. strDateTime = strtok(NULL, "/");
  78. break;
  79. case 1:
  80. strDateTime = strtok(NULL, "-");
  81. break;
  82. case 2:
  83. strDateTime = strtok(NULL, ":");
  84. break;
  85. }
  86. }
  87. if ((strLen[0] == 2) && (strLen[1] == 2) && (strLen[2] == 4)
  88. && (strLen[3] == 2) && (strLen[4] == 2)) {
  89. free(tmpPtr);
  90. tmpPtr = NULL;
  91. tmpStr = NULL;
  92. //return DT_SUCCESS;
  93. return true;
  94. }
  95. free(tmpPtr);
  96. tmpPtr = NULL;
  97. tmpStr = NULL;
  98. throw DtIllegalTimeDateException();
  99. return false;
  100. }
  101.  
  102. int dateTimeToMinutes(DateTime datetime) {
  103. char *tmpStr = (char*) malloc(strlen(datetime) + 1);
  104. char *tmpPtr = tmpStr;
  105. if (tmpStr == NULL) {
  106. throw DtIllegalTimeDateException();
  107. return 0;
  108. }
  109. strcpy(tmpStr, datetime);
  110. DateTime strDateTime = strtok(tmpStr, " /-:");
  111. int resultArray[DATE_TIME_FORMAT] = { 0 };
  112. for (int i = 0; strDateTime && i < DATE_TIME_FORMAT; i++) {
  113. resultArray[i] = atoi(strDateTime);
  114. strDateTime = strtok(NULL, " /-:");
  115. }
  116. int resultInMinutes = 0;
  117. if (resultArray[0] >= MIN_DAY && resultArray[0] <= MAX_DAY
  118. && resultArray[1] >= MIN_MONTH && resultArray[1] <= MAX_MONTH
  119. && resultArray[3] >= 0 && resultArray[3] <= MAX_HOURS
  120. && resultArray[4] >= 0 && resultArray[4] <= MAX_MINUTES
  121. && valiDateFormat(datetime)) {
  122. resultInMinutes = resultArray[0] * MINUTES_IN_DAY
  123. + resultArray[1] * DAYS_IN_MONTH * MINUTES_IN_DAY
  124. + resultArray[2] * MINUTES_IN_YEAR
  125. + resultArray[3] * MINUTES_IN_HOUR + resultArray[4];
  126. free(tmpPtr);
  127. tmpStr = NULL;
  128. return resultInMinutes;
  129. }
  130. free(tmpPtr);
  131. tmpStr = NULL;
  132. throw DtIllegalTimeDateException();
  133. return 0;
  134. }
  135.  
  136.  
  137. int main() {
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement