-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_wrap.cpp
124 lines (118 loc) · 2.36 KB
/
date_wrap.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
#include <exception>
#include "date_wrap.h"
using mtm::DateWrap;
using std::ostream;
using mtm::NegativeDays;
using mtm::InvalidDate;
using std::bad_alloc;
DateWrap::DateWrap(int day, int month, int year)
{
if(day <= 0 || day > this->DAYS_IN_MONTH || month <= 0 || month > this->MONTHS_IN_YEAR)
{
throw InvalidDate();
}
day_data = day;
month_data= month;
year_data = year;
}
int DateWrap::day() const{
return day_data;
}
int DateWrap::month() const{
return month_data;
}
int DateWrap::year() const{
return year_data;
}
Date DateWrap::turnIntoDate() const
{
return dateCreate(day_data, month_data, year_data);
}
int DateWrap::dateWrapCompare(const DateWrap& date) const
{
Date this_date = this->turnIntoDate();
if(this_date == NULL)
{
throw bad_alloc();
}
Date other_date = date.turnIntoDate();
if(other_date == NULL)
{
dateDestroy(this_date);
throw bad_alloc();
}
int compare_result = dateCompare(this_date, other_date);
dateDestroy(this_date);
dateDestroy(other_date);
return compare_result;
}
bool DateWrap::operator==(const DateWrap& date)const{
return this->dateWrapCompare(date) == 0;
}
bool DateWrap::operator!=(const DateWrap& date)const{
return this->dateWrapCompare(date) != 0;
}
bool DateWrap::operator<=(const DateWrap& date)const{
return this->dateWrapCompare(date) <= 0;
}
bool DateWrap::operator>=(const DateWrap& date)const{
return this->dateWrapCompare(date) >= 0;
}
bool DateWrap::operator<(const DateWrap& date)const{
return this->dateWrapCompare(date) < 0;
}
bool DateWrap::operator>(const DateWrap& date)const{
return this->dateWrapCompare(date) > 0;
}
DateWrap DateWrap::operator++(int)
{
DateWrap tmp(*this);
if(day_data < DAYS_IN_MONTH)
{
day_data++;
return tmp;
}
if(month_data < MONTHS_IN_YEAR)
{
month_data++;
day_data = 1;
return tmp;
}
year_data++;
month_data = 1;
day_data = 1;
return tmp;
}
DateWrap DateWrap::operator+=(int days)
{
if(days < 0)
{
throw NegativeDays();
}
for (int i = 0; i < days; i++)
{
(*this)++;
}
return *this;
}
DateWrap DateWrap::operator+(int days) const
{
if(days < 0)
{
throw NegativeDays();
}
DateWrap copy(*this);
return copy += days;
}
namespace mtm
{
DateWrap operator+(int days, const DateWrap& date)
{
return date + days;
}
ostream& operator<<(ostream& os, const DateWrap& date)
{
os << date.day() << "/" << date.month() << "/" << date.year();
return os;
}
}