-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
63 lines (58 loc) · 1.93 KB
/
Utils.cpp
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
//
// Created by orjie on 28/03/2023.
//
#include "Utils.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <algorithm>
namespace utils {
int readInt(const std::string& message) {
int input;
while (true) {
std::cout << message;
std::string line;
if (!std::getline(std::cin, line)) {
throw std::runtime_error("Error: failed to read input");
}
try {
input = std::stoi(line);
break;
} catch (std::invalid_argument& e) {
std::cout << "Invalid input, please enter an integer.\n";
} catch (std::out_of_range& e) {
std::cout << "Input out of range, please enter an integer.\n";
}
}
return input;
}
std::string readString(const std::string& message) {
std::cout << message;
std::string line;
if (!std::getline(std::cin, line)) {
std::cerr << "Error: failed to read input\n";
exit(1);
}
return line;
}
//from https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
//and https://stackoverflow.com/questions/16357999/current-date-and-time-as-string
//and https://en.cppreference.com/w/cpp/chrono/c/time
std::string getCurrentDateTime() {
// Get the current time
std::time_t now = std::time(nullptr);
// Convert it to a struct tm
std::tm time_info = *std::localtime(&now);
// Create a string stream for formatting the output
std::ostringstream oss;
// Format the output as "YYYY-MM-DD HH_MM_SS"
oss << std::put_time(&time_info, "%Y-%m-%d %H:%M:%S");
std::string str = oss.str();
// Replace invalid characters with '_'
std::replace(str.begin(), str.end(), ':', '_');
return str;
}
}