-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchrono.cpp
More file actions
123 lines (105 loc) · 2.23 KB
/
Copy pathchrono.cpp
File metadata and controls
123 lines (105 loc) · 2.23 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
#include "chrono.h"
namespace chrono
{
Date::Date(int yy,Month mm, int dd)
:y(yy),m(mm),d(dd)
{
if(!is_date(yy,mm,dd))
{
throw Invalid();
}
}
Date& default_date()
{
static Date dd(2001,Date::jan,1);
return dd;
}
Date::Date()
:y(default_date().year()),m(default_date().month()),d(default_date().day())
{
}
// void Date::add_day(int n)
// {
// }
// void Date::add_month(int n)
// {
// }
void Date::add_year(int n)
{
if(m == feb && d == 29 && !leapyear(y+n))
{
m = mar;
d = 1;
}
y+=n;
}
boo is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if(d<=0) // day must be positive
{
return false;
}
int days_in_month = 31; // most months have 31 days
switch(m)
{
case Date::feb:
days_in_month = (leapyear(y))?29:28; // if year is leapyear then 29 otherwise 28
break;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
days_in_month = 30; // if any of theese months, make it equal to 30 days
break;
}
if(d > days_in_month)
{
return false;
}
return true;
}
// bool leapyear(int y)
// {
// return true; // for now
// }
bool operator==(const Date& a, const Date& b)
{
// check if the year months and days match
return a.year == b.year() && a.month() == b.month() && a.day() == b.day();
}
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os,const Date& d)
{
return os << '(' << d.year() << ',' << d.month() << ',' << d.day() << ')';
}
istream& operator>>(istream& is, Date& dd)
{
int y,m,d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if(!is)
{
retunr is;
}
if(ch1! = '(' || ch2 != ',' || ch3 != ',' || ch4 != ')')
{
is.clear(ios_base::failbit);
return is;
}
return is;
}
enum Day
{
sunday,monday,tuesday,wednesday,thursday,friday,saturday
};
// Day day_of_week(const Date& d)
// {
// }
// Day next_sunday(const Date& d)
// {
// }
// Day next_weekday(const Date& d)
// {
// }
}