This repository was archived by the owner on Nov 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
66 lines (55 loc) · 1.65 KB
/
date.cpp
File metadata and controls
66 lines (55 loc) · 1.65 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
#include "Date.h"
Date::Date(unsigned short day = 1 , unsigned short month = 1, int year = 2000): _day(day), _month(month), _year(year){
if(!isValid()) throw "Date isn`t valid";
}
Date::~Date(void){
}
bool Date::isValid()
{
if(_month < 1 || _month > 12) return false;
if(_day < 1 || _day > monthDays(_month, _year)) return false;
return true;
}
bool Date::isLeapYear(int year)const{
if ((year % 400 == 0) || ( (year % 4 == 0) && (year % 100 != 0) ))
return true;
return false;
}
int Date::operator- (const Date& other){
int counter = 0;
int year = other._year;
int z = 1;
if(_year < other._year) z = -1;
while(year!=_year){
if(isLeapYear(year+= z)) counter+=366;
else counter+=365;
}
counter += dayOfYear()*z;
counter -= other.dayOfYear()*z;
return counter*z;
}
bool Date::operator< (const Date& other){
if(_year * 10000 + _month * 100 + _day < other._year * 10000 + other._month * 100 + other._day) return true;
return false;
}
bool Date::operator> (const Date& other){
if(_year * 10000 + _month * 100 + _day > other._year * 10000 + other._month * 100 + other._day) return true;
return false;
}
int Date::monthDays(int month, int year)const{
if( month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
return 31;
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
if(isLeapYear(year))
return 29;
return 28;
}
int Date::dayOfYear()const{
int day = 0;
for(int i = 1; i < _month; i++){
day += monthDays(i, _year);
}
return _day + day;
}