-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.cpp
More file actions
110 lines (89 loc) · 2.09 KB
/
time.cpp
File metadata and controls
110 lines (89 loc) · 2.09 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
#include "time.h"
#include <iomanip>
Time::Time() : hours(12), minutes(0), pm(false) {}
Time::Time(const int& hours, const int& minutes, const bool& is_pm)
{
if (!Set(hours, minutes, is_pm))
*this = Time();
}
Time::Time(const Time& other)
: hours(other.hours), minutes(other.minutes), pm(other.pm) {}
Time& Time::operator=(const Time& rhs)
{
if (this != &rhs)
{
this->hours = rhs.hours;
this->minutes = rhs.minutes;
this->pm = rhs.pm;
}
return *this;
}
Time::Time(Time&& other)
: hours(other.hours), minutes(other.minutes), pm(other.pm) {}
Time& Time::operator=(Time&& rhs)
{
if (this != &rhs)
{
this->hours = rhs.hours;
this->minutes = rhs.minutes;
this->pm = rhs.pm;
}
return *this;
}
Time::~Time() {}
bool Time::Set(const int& h, const int& m, const bool& is_pm)
{
if (h < 1 || h > 12 || m < 0 || m >= 60)
return false;
this->hours = h;
this->minutes = m;
this->pm = is_pm;
return true;
}
const int Time::getHours() const
{
return this->hours;
}
const int Time::getMinutes() const
{
return this->minutes;
}
const bool Time::isPM() const
{
return this->pm;
}
bool operator<(const Time& lhs, const Time& rhs)
{
if (lhs.pm != rhs.pm)
return lhs.pm < rhs.pm;
if (lhs.hours != rhs.hours)
return lhs.hours < rhs.hours;
return lhs.minutes < rhs.minutes;
}
bool operator>(const Time& lhs, const Time& rhs)
{
return rhs < lhs;
}
bool operator<=(const Time& lhs, const Time& rhs)
{
return !(lhs > rhs);
}
bool operator>=(const Time& lhs, const Time& rhs)
{
return !(lhs < rhs);
}
bool operator==(const Time& lhs, const Time& rhs)
{
return lhs.hours == rhs.hours && lhs.minutes == rhs.minutes && lhs.pm == rhs.pm;
}
bool operator!=(const Time& lhs, const Time& rhs)
{
return !(lhs == rhs);
}
std::ostream& operator<<(std::ostream& os, const Time& time)
{
os << std::setw(2) << std::setfill('0') << time.hours << ":"
<< std::setw(2) << std::setfill('0') << time.minutes << " "
<< (time.pm ? "PM" : "AM");
return os;
}