-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
83 lines (68 loc) · 1.53 KB
/
Copy pathdate.cpp
File metadata and controls
83 lines (68 loc) · 1.53 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
#include "date.hh"
#include <iostream>
Date::Date(unsigned int day, unsigned int month, unsigned int year):
day_(day), month_(month), year_(year)
{
if ( month_ > 12 || month_ < 1){
month_ = 1;
}
if ( day_ > month_sizes[month_ - 1]
|| (month_ == 2 && is_leap_year()
&& day > month_sizes[month - 1 ] + 1) ){
day_ = 1;
}
}
Date::~Date()
{
}
void Date::advance_by_period_length()
{
advance_by(PERIOD_LENGTH);
}
void Date::advance_by(unsigned int days)
{
day_ = day_ + days;
while ( day_ > month_sizes[month_ - 1] ){
if ( month_ == 2 && day_ == 29 ){
return;
}
day_ = day_ - month_sizes[month_ - 1];
if ( month_ == 2 && is_leap_year() ){
day_--;
}
month_++;
if ( month_ > 12 ){
month_ = month_ - 12;
year_++;
}
}
}
void Date::print() const
{
std::cout << day_ << "." << month_ << "." << year_;
}
unsigned int Date::get_day() const
{
return day_;
}
unsigned int Date::get_month() const
{
return month_;
}
unsigned int Date::get_year() const
{
return year_;
}
bool Date::operator==(const Date &rhs) const
{
return day_ == rhs.day_ && month_ == rhs.month_ && year_ == rhs.year_ ;
}
bool Date::operator<(const Date &rhs) const
{
return (year_ * 10000 + month_ * 100 + day_ ) <
( rhs.year_ * 10000 + rhs.month_ * 100 + rhs.day_);
}
bool Date::is_leap_year() const
{
return (year_ % 4 == 0) && (!(year_ % 100 == 0) || (year_ % 400 == 0));
}