-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_span.cpp
More file actions
126 lines (102 loc) · 3.19 KB
/
Copy pathtime_span.cpp
File metadata and controls
126 lines (102 loc) · 3.19 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
124
125
126
#include "time_span.h"
#include <cmath>
#include <iostream>
// Convert decimal values of hours to minutes, and decimal values of minutes
// to seconds. Everything is added to seconds, and then formatted to a normal
// TimeSpan using the formatTime() method.
TimeSpan::TimeSpan(double hours, double minutes, double seconds)
: hours_(0),
minutes_(0),
seconds_(((int(hours) * 60) * 60) +
((int)std::round(minutes + (((hours - int(hours)) * 60))) * 60) +
(int)std::round(seconds + ((minutes - int(minutes)) * 60))) {
FormatTime();
}
TimeSpan::TimeSpan(double minutes, double seconds)
: TimeSpan(0.0, minutes, seconds) {}
TimeSpan::TimeSpan(double seconds) : TimeSpan(0.0, 0.0, seconds) {}
TimeSpan::TimeSpan() : TimeSpan(0.0, 0.0, 0.0) {}
int TimeSpan::hours() const {
return hours_;
}
int TimeSpan::minutes() const {
return minutes_;
}
int TimeSpan::seconds() const {
return seconds_;
}
// Converts seconds into minutes and minutes into hours using modulo.
void TimeSpan::FormatTime() {
minutes_ += seconds_ / 60;
seconds_ %= 60;
hours_ += minutes_ / 60;
minutes_ %= 60;
if (hours_ > 0 && minutes_ < 0) {
hours_--;
minutes_ += 60;
}
if (minutes_ > 0 && seconds_ < 0) {
minutes_--;
seconds_ += 60;
}
}
void TimeSpan::Set_Time(int hours, int minutes, int seconds) {
hours_ = hours;
minutes_ = minutes;
seconds_ = seconds;
FormatTime();
}
// Converts everything to seconds
int TimeSpan::ToSeconds() const {
return ((hours_ * 60) * 60) + (minutes_ * 60) + seconds_;
}
TimeSpan& TimeSpan::operator+=(TimeSpan const& ts) & {
Set_Time(hours_ += ts.hours_, minutes_ += ts.minutes_,
seconds_ += ts.seconds_);
return *this;
}
TimeSpan& TimeSpan::operator-=(TimeSpan const& ts) & {
Set_Time(hours_ -= ts.hours_, minutes_ -= ts.minutes_,
seconds_ -= ts.seconds_);
return *this;
}
TimeSpan TimeSpan::operator-() {
return TimeSpan(-hours_, -minutes_, -seconds_);
}
bool TimeSpan::operator<=(TimeSpan const& ts) & {
return ToSeconds() <= ts.ToSeconds();
}
bool TimeSpan::operator>=(TimeSpan const& ts) & {
return ToSeconds() >= ts.ToSeconds();
}
bool TimeSpan::operator<(TimeSpan const& ts) & {
return ToSeconds() < ts.ToSeconds();
}
bool TimeSpan::operator>(TimeSpan const& ts) & {
return ToSeconds() > ts.ToSeconds();
}
bool operator==(TimeSpan const& ts1, TimeSpan const& ts2) {
return (ts1.hours_ == ts2.hours_) && (ts1.minutes_ == ts2.minutes_) &&
(ts1.seconds_ == ts2.seconds_);
}
bool operator!=(TimeSpan const& ts1, TimeSpan const& ts2) {
return !(ts1 == ts2);
}
TimeSpan operator+(TimeSpan ts1, TimeSpan const& ts2) {
ts1 += ts2;
return ts1;
}
TimeSpan operator-(TimeSpan ts1, TimeSpan const& ts2) {
ts1 -= ts2;
return ts1;
}
std::ostream& operator<<(std::ostream& os, TimeSpan const& ts) {
return os << "Hours: " << ts.hours() << ", Minutes: " << ts.minutes()
<< ", Seconds: " << ts.seconds();
}
std::istream& operator>>(std::istream& is, TimeSpan& ts) {
int hours, minutes, seconds;
is >> hours >> minutes >> seconds;
ts.Set_Time(hours, minutes, seconds);
return is;
}