-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date.cpp
137 lines (115 loc) · 2.58 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
/*
Date.cpp
Author: M00851681
Created: 4/1/24
Updated: 14/1/24
*/
#include "Date.h"
#include <ctime>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
Date::Date()
{
}
Date::Date(int day, int month, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
int Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
void Date::addDays(int numberDays){
std::array<int, 12> monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int a = 0; a < numberDays; ++a){
day = day + 1;
if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0)))
{
if(day > 29){
day = 1;
month = month + 1;
}
else if (day > monthDays[month])
{
day = 1;
month = month + 1;
if (month > 12)
{
month = 1;
year = year + 1;
}
}
}
}
}
Date Date::getCurrentDate()
{
std::time_t current_time = std::time(nullptr);
tm* time = std::localtime(¤t_time);
int day = time->tm_mday;
// month 0-11
int month = time->tm_mon + 1;
// years since 1900
int year = time->tm_year + 1900;
return Date(day, month, year);
}
bool Date::operator>(Date& other)
{
if (year > other.year)
{
return true;
}
else if (year == other.year)
{
if (month > other.month)
{
return true;
}
else if (month == other.month)
{
if (day > other.day)
{
return true;
}
}
}
return false;
}
int Date::totalDays()
{
std::array<int, 12> monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
int totalCount = day;
for (int mon = 0; mon < month - 1; ++mon)
{
totalCount = totalCount + monthDays[mon];
}
if(month > 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 ==0)))
{
totalCount = totalCount + 1;
}
totalCount = totalCount + (year * 365);
totalCount = totalCount + ((year / 4) - (year / 100) + (year / 400));
return totalCount;
}
int Date::daysAfter(Date& other)
{
return this->totalDays() - other.totalDays() ;
}
std::ostream& operator<<(std::ostream& out, Date& date)
{
out << date.getDay() << "/" << date.getMonth() << "/" << date.getYear();
return out;
}