Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "datetime.h"
- #ifndef DATETIME_H_
- #define DATETIME_H_
- #include <exception>
- namespace RabQavSystem {
- typedef char* DateTime;
- #define DATE_TIME_FORMAT 5
- class DtIllegalTimeDateException : public std::exception{};
- int dateTimeToMinutes(DateTime datetime);
- int dateTimeDifference(DateTime datetime1, DateTime datetime2);
- }
- #endif /* DATETIME_H_ */
- "datetime.cpp"
- #include "datetime.h"
- #include <stdlib.h>
- #include <assert.h>
- #include <string.h>
- #include <malloc.h>
- #include <exception>
- using namespace RabQavSystem;
- const int MIN_DAY = 1;
- const int MAX_DAY = 31;
- const int INVALID_MONTH = 0;
- const int MIN_MONTH = 1;
- const int MAX_MONTH = 12;
- const int MAX_HOURS = 23;
- const int MAX_MINUTES = 59;
- const int DAYS_IN_YEAR = 365;
- const int MINUTES_IN_HOUR = 60;
- const int MINUTES_IN_DAY = 24 * 60;
- const int DAYS_IN_MONTH = 31;
- const int MINUTES_IN_YEAR = 365 * 24 * 60;
- int dateTimeDifference(DateTime datetime1, DateTime datetime2) {
- if (datetime1 == NULL || datetime2 == NULL) {
- throw DtIllegalTimeDateException();
- return 0;
- }
- int dateTimeInMinutes1 = static_cast<int>(dateTimeToMinutes(datetime1));
- int dateTimeInMinutes2 = static_cast<int>(dateTimeToMinutes(datetime2));
- int diffInMinutes = ((dateTimeInMinutes2) - (dateTimeInMinutes1));
- if ((diffInMinutes) >= 0) {
- return diffInMinutes;
- } else {
- throw DtIllegalTimeDateException();
- return 0;
- }
- }
- static bool valiDateFormat(DateTime datetime) {
- DateTime tmpStr = static_cast<DateTime>(malloc(strlen(datetime) + 1));
- DateTime tmpPtr = tmpStr;
- if (tmpStr == NULL) {
- throw DtIllegalTimeDateException();
- return false;
- }
- strcpy(tmpStr, datetime);
- DateTime strDateTime = strtok(tmpStr, "/");
- int strLen[DATE_TIME_FORMAT] = { 0 };
- for (int i = 0; i < DATE_TIME_FORMAT; i++) {
- strLen[i] = strlen(strDateTime);
- switch (i) {
- case 0:
- strDateTime = strtok(NULL, "/");
- break;
- case 1:
- strDateTime = strtok(NULL, "-");
- break;
- case 2:
- strDateTime = strtok(NULL, ":");
- break;
- }
- }
- if ((strLen[0] == 2) && (strLen[1] == 2) && (strLen[2] == 4)
- && (strLen[3] == 2) && (strLen[4] == 2)) {
- free(tmpPtr);
- tmpPtr = NULL;
- tmpStr = NULL;
- //return DT_SUCCESS;
- return true;
- }
- free(tmpPtr);
- tmpPtr = NULL;
- tmpStr = NULL;
- throw DtIllegalTimeDateException();
- return false;
- }
- int dateTimeToMinutes(DateTime datetime) {
- char *tmpStr = (char*) malloc(strlen(datetime) + 1);
- char *tmpPtr = tmpStr;
- if (tmpStr == NULL) {
- throw DtIllegalTimeDateException();
- return 0;
- }
- strcpy(tmpStr, datetime);
- DateTime strDateTime = strtok(tmpStr, " /-:");
- int resultArray[DATE_TIME_FORMAT] = { 0 };
- for (int i = 0; strDateTime && i < DATE_TIME_FORMAT; i++) {
- resultArray[i] = atoi(strDateTime);
- strDateTime = strtok(NULL, " /-:");
- }
- int resultInMinutes = 0;
- if (resultArray[0] >= MIN_DAY && resultArray[0] <= MAX_DAY
- && resultArray[1] >= MIN_MONTH && resultArray[1] <= MAX_MONTH
- && resultArray[3] >= 0 && resultArray[3] <= MAX_HOURS
- && resultArray[4] >= 0 && resultArray[4] <= MAX_MINUTES
- && valiDateFormat(datetime)) {
- resultInMinutes = resultArray[0] * MINUTES_IN_DAY
- + resultArray[1] * DAYS_IN_MONTH * MINUTES_IN_DAY
- + resultArray[2] * MINUTES_IN_YEAR
- + resultArray[3] * MINUTES_IN_HOUR + resultArray[4];
- free(tmpPtr);
- tmpStr = NULL;
- return resultInMinutes;
- }
- free(tmpPtr);
- tmpStr = NULL;
- throw DtIllegalTimeDateException();
- return 0;
- }
- int main() {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement