From f21cd087821f24b48885677162038221aa3dc155 Mon Sep 17 00:00:00 2001 From: theskywinds Date: Fri, 27 Jun 2025 12:38:11 +0200 Subject: [PATCH] Finished program & implemented way to install via cmake --install --- CMakeLists.txt | 4 ++++ include/distime.h | 2 +- src/distime.cpp | 45 +++++++++++++++++++++++++++++++++++---------- src/main.cpp | 23 ++++++++++++++++------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 757ab9e..e612fd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,4 +25,8 @@ target_compile_options(${PROJECT_NAME} PRIVATE -Wconversion # Warn on type conversions that may lose data -Wsign-conversion # Warn on sign conversions -Wnull-dereference # Warn about null pointer dereference +) + +install(TARGETS ${CMAKE_PROJECT_NAME} + RUNTIME DESTINATION bin ) \ No newline at end of file diff --git a/include/distime.h b/include/distime.h index 4c5bfb2..e54f76e 100644 --- a/include/distime.h +++ b/include/distime.h @@ -8,6 +8,6 @@ #include 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 diff --git a/src/distime.cpp b/src/distime.cpp index 018abcc..b063936 100644 --- a/src/distime.cpp +++ b/src/distime.cpp @@ -5,17 +5,21 @@ #include 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(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; diff --git a/src/main.cpp b/src/main.cpp index 7c0842a..3797f7a 100644 --- a/src/main.cpp +++ b/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: '; return EXIT_SUCCESS; -} - catch (const std::invalid_argument &e) { - std::cerr << "Error: " << e.what(); - } + + }