-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateManager.cpp
More file actions
133 lines (114 loc) · 3.92 KB
/
DateManager.cpp
File metadata and controls
133 lines (114 loc) · 3.92 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "DateManager.h"
int DateManager :: getCurrentDate() {
int currentDate = 0;
year = getCurrentYear();
currentDate = (year * 10000);
month = getCurrentMonth();
currentDate += (month * 100);
day = getCurrentDay();
currentDate += day;
return currentDate;
}
int DateManager :: getCurrentYear() {
time(&calculatedTime); //Wywolana jest funkcja time(&calculatedTime), ktora pobiera aktualny czas w postaci liczby sekund od pewnej stalej daty (czasu Unix).Zmienna calculatedTime przechowuje wynik tej funkcji.
data = localtime(&calculatedTime); //funkcja localtime(&calculatedTime) przeksztalca wartosc liczbowa reprezentujaca czas w strukture tm
year = data -> tm_year+1900; //odnosimy sie do pola tm_year struktury data, ktore przechowuje liczbe lat od 1900 roku. Dodajac 1900 do tej liczby otrzymujemy wlasciwy rok.
return year;
}
int DateManager :: getCurrentMonth() {
time(&calculatedTime);
data = localtime(&calculatedTime);
month = data -> tm_mon+1; //odnosimy sie do pola tm_mon struktury data, ktore przechowuje numer miesiaca od 0 (styczen) do 11 (grudzien). Dodajac 1 do tej liczby otrzymujemy wlasciwy numer miesiaca (1 - styczen, 2 - luty, ...)
return month;
}
int DateManager :: getCurrentDay() {
time(&calculatedTime);
data = localtime(&calculatedTime);
day = data -> tm_mday;
return day;
}
bool DateManager :: isDateCorrect(string date) {
int year = (date[0]-'0')*1000 + (date[1]-'0')*100 + (date[2]-'0')*10 + (date[3]-'0');
int month = (date[5]-'0')*10 + (date[6]-'0');
int day = (date[8]-'0')*10 + (date[9]-'0');
if (!isValidFormatDate(date)) {
return false;
}
if (!isValidYear (year)) {
return false;
}
if (!isValidMonth(month) || month > getCurrentMonth()) {
return false;
}
if (day >= 1 && day <= 31) {
if (isValidDay(day, month, year) == true) {
if (isFutureDate(date)) {
return false;
}
return true;
} else {
return false;
}
}
return false;
}
bool DateManager :: isValidYear (int year) {
if (year > getCurrentYear() || year < MIN_VALID_YR) {
return false;
}
return true;
}
bool DateManager :: isValidMonth(int month) {
if (month < 1 || month > 12) {
return false;
}
return true;
}
bool DateManager :: isValidDay (int day, int month, int year) {
if (month == 2) {
if ((isLeap(year) == true) && (day == 29)) {
return true;
} else if (day >= 1 && day <= 28) {
return true;
} else if (day < 1 || day > 29) {
return false;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day >= 1 && day <= 30) {
return true;
} else return false;
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day >= 1 && day <= 31) {
return true;
} else return false;
}
return false;
}
bool DateManager :: isFutureDate(string stringDate) {
int date = SupportMethods :: convertStringToInt (SupportMethods :: removeDashFromDate(stringDate));
return date > getCurrentDate();
}
bool DateManager :: isValidFormatDate (string date) {
if ((date.size() != 10) || (date[4] != '-') || (date[7] != '-')) {
return false;
}
for (int i = 0; i < 10; i++) {
if (i == 4 || i == 7) {
continue;
}
if (!isdigit(date[i])) {
return false;
}
}
return true;
}
bool DateManager :: isLeap(int year) {
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else return false;
}
void DateManager :: showDate(Date dateOb) {
cout << endl << "Year: " << dateOb.getYear() << endl;
cout << "Month: " << dateOb.getMonth() << endl;
cout << "Day: " << dateOb.getDay() << endl;
}