-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
454 lines (392 loc) · 15.1 KB
/
Copy pathmain.cpp
File metadata and controls
454 lines (392 loc) · 15.1 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#define _CRT_SECURE_NO_WARNINGS
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <json.hpp>
#include <ctime>
#include <iomanip>
#include "tabulate.hpp"
using namespace tabulate;
using json = nlohmann::json;
const std::string SETTINGS_FILE = "../../settings.json";
const std::string DAYS_OFF_FILE = "../../days_off.json";
// Formats hours as a string, converting to days and remaining hours if hours > 8
std::string format_hrs(double hours) {
std::ostringstream oss;
if (hours >= 8.0) {
int days = static_cast<int>(hours / 8.0);
double rem_hours = hours - (days * 8.0);
oss << days << " day" << (days == 1 ? "" : "s");
if (rem_hours > 0.01) {
oss << " " << std::fixed << std::setprecision(1) << rem_hours << " hr" << (rem_hours == 1.0 ? "" : "s");
}
} else {
oss << std::fixed << std::setprecision(1) << hours << " hr" << (hours == 1.0 ? "" : "s");
}
return oss.str();
}
static bool entry_exists(const nlohmann::json& days_off, const std::string& key, const std::string& date) {
for (const auto& entry : days_off) {
if (entry.contains(key) && entry[key] == date) {
return true;
}
}
return false;
}
// this needs to check if the date provided is contained within any days off entries
// including date, start_date, end_date, and between start_date & end_date
static bool is_day_off(const nlohmann::json& days_off, const std::string& date) {
// check specific dates
// check date ranges
}
std::tm date_string_to_tm(const std::string& date_str) {
std::tm tm = {};
std::istringstream ss(date_str);
ss >> std::get_time(&tm, "%Y-%m-%d");
if (ss.fail()) {
throw std::runtime_error("Invalid date format. Use YYYY-MM-DD.");
}
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
return tm;
}
std::string tm_to_date_string(const std::tm& tm) {
std::ostringstream oss;
oss << std::put_time(&tm, "%Y-%m-%d");
return oss.str();
}
bool is_weekday(const std::tm& tm) {
std::time_t t = std::mktime(const_cast<std::tm*>(&tm));
std::tm* lt = std::localtime(&t);
return lt->tm_wday != 0 && lt->tm_wday != 6; // not Sunday or Saturday
}
int working_days_elapsed_since(const std::tm& start_date) {
std::time_t now = std::time(nullptr);
std::tm today = *std::localtime(&now);
std::time_t start = std::mktime(const_cast<std::tm*>(&start_date));
std::time_t end = std::mktime(&today);
int weekdays_elapsed = 0;
for (std::time_t t = start; t <= end; t += 86400) {
std::tm* tm = std::localtime(&t);
if (is_weekday(*tm)) {
weekdays_elapsed++;
}
}
return weekdays_elapsed;
}
double calculate_accrued_hours_to(const json& days_off, const std::tm& from_date, const std::tm& to_date, double accrual_rate) {
std::time_t start = std::mktime(const_cast<std::tm*>(&from_date));
std::time_t end = std::mktime(const_cast<std::tm*>(&to_date));
double accrued = 0.0;
for (std::time_t t = start; t <= end; t += 86400) {
std::tm* tm = std::localtime(&t);
if (is_weekday(*tm)) {
accrued += accrual_rate;
}
}
return accrued;
}
double calculate_accrued_hours(const json& days_off, const std::tm& start_date, double accrual_rate) {
std::time_t now = std::time(nullptr);
std::tm today = *std::localtime(&now);
std::time_t start = std::mktime(const_cast<std::tm*>(&start_date));
std::time_t end = std::mktime(&today);
double accrued = 0.0;
for (std::time_t t = start; t <= end; t += 86400) {
std::tm* tm = std::localtime(&t);
if (is_weekday(*tm)) {
accrued += accrual_rate;
}
}
return accrued;
}
double calculate_hours_of_days_off(const json& days_off) {
double used_hours = 0.0;
for (const auto& entry : days_off) {
if (entry.contains("date")) {
used_hours += entry.value("hours", 8.0);
}
else if (entry.contains("start_date") && entry.contains("end_date")) {
std::tm start_tm = date_string_to_tm(entry["start_date"]);
std::tm end_tm = date_string_to_tm(entry["end_date"]);
double hours_per_day = entry.value("hours_per_day", 8.0);
std::time_t start = std::mktime(&start_tm);
std::time_t end = std::mktime(&end_tm);
for (std::time_t t = start; t <= end; t += 86400) {
std::tm* current_tm = std::localtime(&t);
if (is_weekday(*current_tm)) {
used_hours += hours_per_day;
}
}
}
}
return used_hours;
}
void save_days_off(const std::string& path, const json& days_off) {
std::ofstream ofs(path);
if (!ofs) {
std::cerr << "Error: Unable to write to " << path << "\n";
return;
}
ofs << days_off.dump(4); // pretty print
}
void list_days_off(const json& days_off) {
if (days_off.empty()) {
std::cout << "No days off recorded.\n";
return;
}
std::cout << "-------------------------------------------------------------------------------\n";
std::cout << std::left << std::setw(25) << "Date/Range"
<< std::right << std::setw(18) << "Type"
<< std::right << std::setw(14) << "Hours"
<< std::right << std::setw(20) << "Reason" << "\n";
std::cout << "-------------------------------------------------------------------------------\n";
for (const auto& entry : days_off) {
std::string reason = entry.value("reason", "");
if (entry.contains("date")) {
std::cout << std::left << std::setw(25) << std::string(entry["date"])
<< std::right << std::setw(18) << "Single Day"
<< std::right << std::setw(14) << entry.value("hours", 8.0)
<< std::right << std::setw(20) << reason
<< "\n";
}
else if (entry.contains("start_date")) {
std::string range = std::string(entry["start_date"]) + " to " + std::string(entry["end_date"]);
std::cout << std::left << std::setw(25) << range
<< std::right << std::setw(18) << "Range"
<< std::right << std::setw(14) << entry.value("hours_per_day", 8.0)
<< std::right << std::setw(20) << reason
<< "\n";
}
}
std::cout << "-------------------------------------------------------------------------------\n";
}
// Print the days that have been taken off using tabulate
void list_days_off_tabulate(const json& days_off) {
std::cout << "Logged Time Off\n";
Table table;
table.add_row({ "Date/Range", "Type", "Time Off", "Reason" });
for (const auto& entry : days_off) {
std::string reason = entry.value("reason", "");
if (entry.contains("date")) {
std::string time_off = format_hrs(entry.value("hours", 8.0));
table.add_row({ std::string(entry["date"]), "Single Day", time_off, reason });
}
else if (entry.contains("start_date")) {
std::string range = std::string(entry["start_date"]) + " to " + std::string(entry["end_date"]);
double hours_per_day = entry.value("hours_per_day", 8.0);
std::tm start_tm = date_string_to_tm(entry["start_date"]);
std::tm end_tm = date_string_to_tm(entry["end_date"]);
std::time_t start = std::mktime(&start_tm);
std::time_t end = std::mktime(&end_tm);
int weekday_count = 0;
for (std::time_t t = start; t <= end; t += 86400) {
std::tm* current_tm = std::localtime(&t);
if (is_weekday(*current_tm)) {
weekday_count++;
}
}
double total_time_off = weekday_count * hours_per_day;
table.add_row({ range, "Range", format_hrs(total_time_off), reason });
}
}
std::cout << table << std::endl;
std::cout << "\n";
}
bool is_future_date(std::tm& input_tm) {
input_tm.tm_hour = 0;
input_tm.tm_min = 0;
input_tm.tm_sec = 0;
std::time_t input_time = std::mktime(&input_tm);
// Get today's date, set to midnight
std::time_t now = std::time(nullptr);
std::tm today_tm = *std::localtime(&now);
today_tm.tm_hour = 0; today_tm.tm_min = 0; today_tm.tm_sec = 0;
std::time_t today_time = std::mktime(&today_tm);
return input_time > today_time;
}
void print_usage() {
std::cout << "Usage (date format used: yyyy-mm-dd):\n"
<< " pto Show available PTO\n"
<< " pto add <date> [hours] Add a day off (default: 8 hours)\n"
<< " pto add_range <start> <end> [hours/day] Add range of days off (default: 8)\n"
<< " pto remove <date> Remove entries matching a date (exact match to 'date' or 'start_date')\n"
<< " pto show_days_off Show all saved days off\n"
<< " pto show_hrs_on <date> Show amount of accrued hours on some future date\n"
<< " pto usage Show this help message\n\n";
}
// TODO: show hours accrued on a date - this can be used to look ahead and know how much vaca time I'll have by
// that date, assuming I don't take any additional time off between now and then.
// Show accrued hours on a specific future date
void show_hrs_on(const json& settings, const json& days_off, const std::string& future_date) {
std::tm future_date_tm = date_string_to_tm(future_date);
// should this matter?
if (!is_weekday(future_date_tm)) {
std::cerr << "Error: Trying to show a date in the future that is a weekend.\n";
return;
}
// check that it is a date in the future
if (is_future_date(future_date_tm)) {
// accrue hours between when i started and then
std::tm start_tm = date_string_to_tm(settings["start_date"]);
double accrual_rate = settings["accrual_rate_per_day"];
double accrued_hours = calculate_accrued_hours_to(days_off, start_tm, future_date_tm, accrual_rate);
double hours_taken_off = calculate_hours_of_days_off(days_off);
double hours_available = accrued_hours - hours_taken_off;
std::cout << "Accrued hours to then: " << format_hrs(hours_available) << " hours.\n";
}
else {
std::cerr << "Error: The date provided is not in the future.\n";
}
}
// Add a single day off
void add_day_off(json& days_off, const std::string& date, double hours, const std::string& reason) {
if (entry_exists(days_off, "date", date)) {
std::cerr << "Error: A time-off entry for " << date << " already exists.\n";
return;
}
std::tm date_tm = date_string_to_tm(date);
if (!is_weekday(date_tm)) {
std::cerr << "Error: Trying to add a date that is a weekend.\n";
return;
}
days_off.push_back({ {"date", date}, {"hours", hours}, {"reason", reason} });
save_days_off(DAYS_OFF_FILE, days_off);
std::cout << "Added day off: " << date << " (" << hours << "h, Reason: " << reason << ")\n";
}
// Add a range of days off
void add_range_days_off(json& days_off, const std::string& start, const std::string& end, double hours_per_day, const std::string& reason) {
if (entry_exists(days_off, "date", start) || entry_exists(days_off, "date", end) ||
entry_exists(days_off, "start_date", start) || entry_exists(days_off, "start_date", end) ||
entry_exists(days_off, "end_date", start) || entry_exists(days_off, "end_date", end)) {
std::cerr << "Error: A time-off entry already exists for the start or end date.\n";
return;
}
std::tm start_tm = date_string_to_tm(start);
if (!is_weekday(start_tm)) {
std::cerr << "Error: Trying to add a start_date that is a weekend.\n";
return;
}
std::tm end_tm = date_string_to_tm(end);
if (!is_weekday(end_tm)) {
std::cerr << "Error: Trying to add a end_date that is a weekend.\n";
return;
}
days_off.push_back({
{"start_date", start},
{"end_date", end},
{"hours_per_day", hours_per_day},
{"reason", reason}
});
save_days_off(DAYS_OFF_FILE, days_off);
std::cout << "Added days off: " << start << " to " << end << " (" << hours_per_day << "h/day, Reason: " << reason << ")\n";
}
// Print the days that have been taken off and a summary
void print_pto_summary(const json& settings, const json& days_off) {
std::tm start_tm = date_string_to_tm(settings["start_date"]);
double accrual_rate = settings["accrual_rate_per_day"];
std::string accrual_rate_str = std::to_string(accrual_rate) + " hours/day";
double accrued_hours_since_hired = calculate_accrued_hours(days_off, start_tm, accrual_rate);
int working_days_since_hired = working_days_elapsed_since(start_tm);
std::string working_days_since_hired_str = std::to_string(working_days_since_hired) + " days";
double hours_taken_off = calculate_hours_of_days_off(days_off);
double hours_available = accrued_hours_since_hired - hours_taken_off;
std::cout << "\n===============================================\n";
std::cout << " Paid Time Off Tracker \n";
std::cout << "===============================================\n\n";
list_days_off_tabulate(days_off);
std::cout << "Summary\n";
Table summary_table;
summary_table.add_row({"Accrual Rate:", accrual_rate_str});
summary_table.add_row({"Working Days Since Hired:", working_days_since_hired_str});
summary_table.add_row({"Time Accrued:", format_hrs(accrued_hours_since_hired)});
summary_table.add_row({"Time Used:", format_hrs(hours_taken_off)});
summary_table.add_row({"Time Balance:", format_hrs(hours_available)});
if (hours_available < 0) {
int days_needed = static_cast<int>(std::ceil(std::abs(hours_available) / accrual_rate));
summary_table.add_row({"Days Needed To Get To 0:", std::to_string(days_needed)});
}
if (hours_available > 40) {
summary_table.add_row({"You Have A Problem", "YES, YOU SHOULD TAKE SOME VACATION!"});
}
std::cout << summary_table << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[]) {
std::cout << std::fixed << std::setprecision(1);
// Show usage if explicitly asked
if (argc >= 2 && std::string(argv[1]) == "usage") {
print_usage();
return 0;
}
// Load settings
std::ifstream settings_ifs(SETTINGS_FILE);
if (!settings_ifs) {
std::cerr << "Error: Cannot open settings file.\n";
return 1;
}
json settings;
settings_ifs >> settings;
// Load or create days_off
json days_off = json::array();
std::ifstream days_off_ifs(DAYS_OFF_FILE);
if (days_off_ifs) {
days_off_ifs >> days_off;
}
// CLI: show hours on the provided date
if (argc >= 3 && std::string(argv[1]) == "show_hrs_on") {
show_hrs_on(settings, days_off, argv[2]);
return 0;
}
// CLI: add single date
if (argc >= 3 && std::string(argv[1]) == "add") {
std::string date = argv[2];
double hours = (argc >= 4) ? std::stod(argv[3]) : 8.0;
std::string reason = (argc >= 5) ? argv[4] : "";
add_day_off(days_off, date, hours, reason);
return 0;
}
// CLI: add date range
if (argc >= 4 && std::string(argv[1]) == "add_range") {
std::string start = argv[2];
std::string end = argv[3];
double hours_per_day = (argc >= 5) ? std::stod(argv[4]) : 8.0;
std::string reason = (argc >= 6) ? argv[5] : "";
add_range_days_off(days_off, start, end, hours_per_day, reason);
return 0;
}
// CLI: list
if (argc >= 2 && std::string(argv[1]) == "show_days_off") {
list_days_off(days_off);
return 0;
}
// CLI: remove by date (matches any "date" or "start_date")
if (argc >= 3 && std::string(argv[1]) == "remove") {
std::string target = argv[2];
auto new_days_off = json::array();
for (const auto& entry : days_off) {
if ((entry.contains("date") && entry["date"] == target) ||
(entry.contains("start_date") && entry["start_date"] == target)) {
continue; // skip
}
new_days_off.push_back(entry);
}
save_days_off(DAYS_OFF_FILE, new_days_off);
std::cout << "Removed entries for date: " << target << "\n";
return 0;
}
if (argc > 1) {
std::cerr << "Unknown command or wrong usage.\n";
print_usage();
return 1;
}
// Default behavior: calculate PTO
if (argc == 1) {
print_pto_summary(settings, days_off);
return 0;
}
return 0;
}