-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
74 lines (56 loc) · 1.33 KB
/
Date.cpp
File metadata and controls
74 lines (56 loc) · 1.33 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
#include "Date.h"
#include <stdexcept>
#include <iostream>
#include <iomanip>
using namespace std;
Date::Date( unsigned int m, unsigned int d)
{
setMonth(m);
setDay(d);
year = 2021;
}
void Date::setMonth(unsigned int m)
{
if(m >= 1 && m <= 12)
month = m;
else
throw std::invalid_argument("Invalid month!");
}
unsigned int Date::getMonth() const
{
return month;
}
void Date::setDay(unsigned int d)
{
if(checkDay(d))
day = d;
else
throw std::invalid_argument("Invalid day for current month and year!");
}
unsigned int Date::getDay() const
{
return day;
}
bool Date::checkDay(unsigned int testDay) const
{
unsigned int daysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(testDay <= daysPerMonth[month])
return true;
if((month == 2 && testDay == 29) &&
(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
return true;
return false;
}
ostream& operator<<(ostream &out , Date &d)
{
out << setfill('0')
<< setw(2) << d.getDay() << "-"
<< setw(2) << d.getMonth() ;
return out;
}
istream &operator>> (istream& in, Date& d)
{
in >> setw(2)>>d.day;
in.ignore();
in >> setw(2)>>d.month;
}