Finished program & implemented way to install via cmake --install
This commit is contained in:
parent
f9d9eae92f
commit
f21cd08782
|
|
@ -26,3 +26,7 @@ target_compile_options(${PROJECT_NAME} PRIVATE
|
|||
-Wsign-conversion # Warn on sign conversions
|
||||
-Wnull-dereference # Warn about null pointer dereference
|
||||
)
|
||||
|
||||
install(TARGETS ${CMAKE_PROJECT_NAME}
|
||||
RUNTIME DESTINATION bin
|
||||
)
|
||||
|
|
@ -8,6 +8,6 @@
|
|||
#include <ctime>
|
||||
|
||||
namespace distime {
|
||||
void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime);
|
||||
void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime, bool shortTime);
|
||||
}
|
||||
#endif //DISTIME_H
|
||||
|
|
|
|||
|
|
@ -5,17 +5,21 @@
|
|||
#include <iostream>
|
||||
|
||||
namespace distime {
|
||||
void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime) {
|
||||
void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime, bool shortTime) {
|
||||
std::string timeInput{};
|
||||
bool continueLoop{true};
|
||||
while (continueLoop) {
|
||||
// Unique formatting of 'cout' should make the output easier to predict
|
||||
std::cout
|
||||
//blank
|
||||
<< "\nCurrent time: " << std::put_time(¤tTime, "%d-%m-%Y %H:%M\n")
|
||||
//blank
|
||||
<< "Day (dd) Month (mm) Year(yyyy) 24 Hour (HH) Minute (MM)\n"
|
||||
<< ">: ";
|
||||
<< "\nCurrent time: " << std::put_time(¤tTime, "%d-%m-%Y %H:%M\n");
|
||||
|
||||
if (shortTime) {
|
||||
std::cout << "24 Hour (HH) Minute (MM)\n";
|
||||
}
|
||||
else {
|
||||
std::cout << "Day (dd) Month (mm) Year(yyyy) 24 Hour (HH) Minute (MM)\n";
|
||||
}
|
||||
std::cout << ">: ";
|
||||
|
||||
std::getline(std::cin, timeInput);
|
||||
|
||||
|
|
@ -26,12 +30,21 @@ namespace distime {
|
|||
|
||||
int spaceCheck = static_cast<int>(std::ranges::count(timeInput.cbegin(), timeInput.cend(), ' '));
|
||||
|
||||
if (spaceCheck != 4) {
|
||||
if (spaceCheck != 4 && shortTime == false) {
|
||||
if (spaceCheck < 4)
|
||||
std::cout << "Too few spaces\n";
|
||||
else if (spaceCheck > 4)
|
||||
std::cout << "Too many spaces\n";
|
||||
std::cout << "Example of a working input: '25 02 2015 06 00' \n";
|
||||
std::cout << "Example of a working input: '25 02 2015 06 00' \n\n";
|
||||
|
||||
std::cout << "Press enter to continue...";
|
||||
std::cin.get();
|
||||
continue;
|
||||
}
|
||||
else if (spaceCheck != 1 && shortTime == true) {
|
||||
std::cout << "Only one space permitted.\n";
|
||||
std::cout << "Example of a working input: '20 00' \n\n";
|
||||
|
||||
std::cout << "Press enter to continue...";
|
||||
std::cin.get();
|
||||
continue;
|
||||
|
|
@ -42,9 +55,21 @@ namespace distime {
|
|||
|
||||
std::istringstream timeStream {timeInput};
|
||||
|
||||
int day, month, year, hour, minute;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
int hour;
|
||||
int minute;
|
||||
|
||||
timeStream >> day >> month >> year >> hour >> minute;
|
||||
if (shortTime) {
|
||||
day = currentTime.tm_mday;
|
||||
month = currentTime.tm_mon + 1;
|
||||
year = currentTime.tm_year + 1900;
|
||||
|
||||
timeStream >> hour >> minute;
|
||||
}
|
||||
else
|
||||
timeStream >> day >> month >> year >> hour >> minute;
|
||||
|
||||
// Assign the values to resultTime
|
||||
resultTime.tm_mday = day;
|
||||
|
|
|
|||
23
src/main.cpp
23
src/main.cpp
|
|
@ -14,7 +14,7 @@ void validateTimeFormatSelection(char timeFormat) {
|
|||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
try {
|
||||
|
||||
CLI::App app{};
|
||||
app.usage("USAGE: distime [OPTIONS]");
|
||||
|
||||
|
|
@ -23,8 +23,14 @@ int main(int argc, char **argv) {
|
|||
->multi_option_policy (CLI::MultiOptionPolicy::Throw)
|
||||
->default_val('f');
|
||||
|
||||
bool shortTime;
|
||||
app.add_flag("-s, --short", shortTime, "Defaults all but hour and minute to current time")
|
||||
->default_val(false)
|
||||
->default_str("false");
|
||||
|
||||
auto formatHelp { [] () {
|
||||
std::cout
|
||||
<< "FORMATS:\n"
|
||||
<< "F - Long format [Friday, June 27, 2025 at 9:00]\n"
|
||||
<< "f - Short Long Format [June 27, 2025 at 9:00]\n"
|
||||
<< "D - Short Format [June 27, 2025]\n"
|
||||
|
|
@ -40,20 +46,23 @@ int main(int argc, char **argv) {
|
|||
|
||||
CLI11_PARSE(app, argc, argv);
|
||||
|
||||
validateTimeFormatSelection(timeFormat);
|
||||
try {
|
||||
validateTimeFormatSelection(timeFormat);
|
||||
}
|
||||
catch (const std::invalid_argument &e) {
|
||||
std::cerr << "Error: " << e.what();
|
||||
}
|
||||
|
||||
std::time_t result = std::time(nullptr);
|
||||
const std::tm currentTime = *std::localtime(&result);
|
||||
[[maybe_unused]] std::tm resultTime = currentTime;
|
||||
|
||||
distime::parseTimeInput(resultTime, currentTime);
|
||||
distime::parseTimeInput(resultTime, currentTime, shortTime);
|
||||
|
||||
std::cout << "Resulting time: " << std::put_time(&resultTime, "%d-%m-%Y %H:%M\n");
|
||||
std::cout << "Time stamp: <t:" << std::mktime(&resultTime) << ':' << timeFormat << '>';
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
catch (const std::invalid_argument &e) {
|
||||
std::cerr << "Error: " << e.what();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user