-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.cpp
167 lines (118 loc) · 3.01 KB
/
Date.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Date.h"
#include <sstream>
#include <ctime>
using namespace std;
Date::Date(int d, int m, int y) {
_day = d;
_month = m;
_year = y;
_df = new DateFormatDE;
}
void Date::setDateFormat(DateFormat *f) {
_df = f;
}
bool Date::operator==(const Date& d) {
return ((this->_day == d._day) && (this->_month == d._month) && (this->_year == d._year));
}
int Date::operator-(const Date& d) {
time_t currSeconds, firstDate, secondDate;
double diffInSeconds;
struct tm *timeData;
time(&currSeconds);
timeData = localtime(&currSeconds);
timeData->tm_year = _year - 1900;
timeData->tm_mon = _month - 1;
timeData->tm_mday = _day;
firstDate = mktime(timeData);
timeData = localtime(&currSeconds);
timeData->tm_year = d._year - 1900;
timeData->tm_mon = d._month - 1;
timeData->tm_mday = d._day;
secondDate = mktime(timeData);
diffInSeconds = difftime(firstDate, secondDate);
return (diffInSeconds / (3600 * 24));
}
int Date::getDayInYear() {
time_t currSeconds;
struct tm *timeData;
time(&currSeconds);
timeData = localtime(&currSeconds);
timeData->tm_year = _year - 1900;
timeData->tm_mon = _month - 1;
timeData->tm_mday = _day;
mktime(timeData);
return timeData->tm_yday + 1;
}
int Date::getDayInWeek() {
time_t currSeconds;
struct tm *timeData;
time(&currSeconds);
timeData = localtime(&currSeconds);
timeData->tm_year = _year - 1900;
timeData->tm_mon = _month - 1;
timeData->tm_mday = _day;
mktime(timeData);
return timeData->tm_wday;
}
int Date::getWeekInYear() {
time_t currSeconds;
struct tm *timeData;
int dayInWeek, diffToThursday, firstThursday;
time(&currSeconds);
timeData = localtime(&currSeconds);
timeData->tm_year = _year - 1900;
timeData->tm_mon = _month - 1;
timeData->tm_mday = _day;
mktime(timeData);
//Wochentag bestimmen
dayInWeek = timeData->tm_wday;
//Differenz zum Donnerstag in der gleichen Woche bestimmen
if (dayInWeek == 0) {
diffToThursday = -3;
}
else if (dayInWeek >= 4) {
diffToThursday = 4 - dayInWeek;
}
else if (dayInWeek < 4) {
diffToThursday = 4 - dayInWeek;
}
//Datum des ersten Donerstags im Januar des übergebenen Jahres bestimmen
firstThursday = (timeData->tm_yday + 1 + diffToThursday) % 7;
if (firstThursday <= 0)
return (timeData->tm_yday + 1 + diffToThursday) / 7;
else
return ((timeData->tm_yday + 1 + diffToThursday) / 7) + 1;
}
bool Date::isLeapYear() {
if (_year % 400 == 0)
return true;
else if (_year % 100 == 0)
return false;
else if (_year % 4 == 0)
return true;
else
return false;
}
int Date::diff(Date d) {
int diff;
diff = *this - d;
if (diff < 0)
return -diff;
else
return diff;
}
string Date::toString() {
return _df->format(this);
}
int Date::getDay() {
return _day;
}
int Date::getMonth() {
return _month;
}
int Date::getYear() {
return _year;
}
string Date::getNameOfDay(int day) {
return _df->getNameOfDay(day);
}