-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.h
152 lines (131 loc) · 3.75 KB
/
Time.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Created by ryanz on 4/24/2022.
//
#ifndef SCHEDULER_TIME_H
#define SCHEDULER_TIME_H
#include <iostream>
#include <sstream>
#include <iomanip>
#include <regex>
using namespace std;
class Time {
int hour;
int minute;
public:
int getHour() const {
return hour;
}
int getMinute() const {
return minute;
}
// convert from 24 hour time to 12 hour time
std::string to12Hour() const {
std::stringstream ss;
if (hour == 0) {
ss << "12";
} else if (hour > 12) {
ss << hour - 12;
} else {
ss << hour;
}
ss << ":" << setfill('0') << setw(2) << minute << " " << (hour < 12 ? "AM" : "PM");
return ss.str();
}
// time comparator ==
bool operator==(const Time& t) const {
return hour == t.hour && minute == t.minute;
}
// time comparator !=
bool operator!=(const Time& t) const {
return !(*this == t);
}
Time() : hour(0), minute(0) {}
Time(int h, int m) : hour(h), minute(m){
// validate
if (h < 0 || h > 23 || m < 0 || m > 59) {
throw std::invalid_argument("invalid time");
}
}
std::string toString() const {
std::stringstream ss;
// convert to 12 hour time
ss << to12Hour();
return ss.str();
}
static Time parse(const std::string &timeStr) {
std::smatch match;
std::regex timePattern("(\\d{1,2}):(\\d{2})(am|pm|AM|PM)");
if (std::regex_search(timeStr, match, timePattern)) {
int hour = std::stoi(match.str(1));
int minute = std::stoi(match.str(2));
std::string meridiem = match.str(3);
// Convert to 24-hour time
if (meridiem == "pm" || meridiem == "PM") {
hour = (hour % 12) + 12;
} else { // AM case
hour %= 12;
}
return Time(hour, minute);
} else {
throw std::invalid_argument("Failed to parse time string");
}
}
// time comparator >
bool operator>(const Time& t) const {
if (hour > t.hour) {
return true;
} else if (hour == t.hour) {
return minute > t.minute;
} else {
return false;
}
}
// time comparator <
bool operator<(const Time& t) const {
if (hour < t.hour) {
return true;
} else if (hour == t.hour) {
return minute < t.minute;
} else {
return false;
}
}
// time comparator >=
bool operator>=(const Time& t) const {
if (hour == t.hour && minute == t.minute) {
return true;
} else if (hour > t.hour) {
return true;
} else if (hour == t.hour) {
return minute >= t.minute;
} else {
return false;
}
}
// time comparator <=
bool operator<=(const Time& t) const {
if (hour == t.hour && minute == t.minute) {
return true;
} else if (hour < t.hour) {
return true;
} else if (hour == t.hour) {
return minute < t.minute;
} else {
return false;
}
}
// overload the << operator
friend ostream& operator<<(ostream& os, const Time& t) {
os << t.toString();
return os;
}
};
// sort time by ending time
bool timeComparator(const Time& lhs, const Time& rhs) {
if (lhs.getHour() == rhs.getHour()) {
return lhs.getMinute() < rhs.getMinute();
} else {
return lhs.getHour() < rhs.getHour();
}
}
#endif //SCHEDULER_TIME_H