-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateConverter.cpp
More file actions
48 lines (39 loc) · 1.1 KB
/
Copy pathDateConverter.cpp
File metadata and controls
48 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "DateConverter.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
int DateConverter::numberOfUsage = 0;
int DateConverter::getNumberOfUsage(){
return DateConverter::numberOfUsage;
}
const char* DateConverter::toString(const Date &d){
char temp[101];
sprintf(temp, "%d-%d-%d", d.getYear(), d.getMonth(), d.getDay());
char* retVal = new char[strlen(temp) + 1];
strcpy(retVal, temp);
return retVal;
}
Date DateConverter::toDate(const char* dateStr){
int strLength = (int) strlen(dateStr);
char seperate[3][10];
int counter = 0;
char temp[10];
int tempCounter = 0;
for(int i = 0; i < strLength; i++){
if(dateStr[i] == '-'){
i++;
strcpy(seperate[counter], temp);
tempCounter = 0;
strcpy(temp, " ");
counter++;
}
temp[tempCounter] = dateStr[i];
tempCounter++;
}
strcpy(seperate[counter], temp);
int year = atoi(seperate[0]);
int month = atoi(seperate[1]);
int day = atoi(seperate[2]);
return Date(day, month, year);
}