Finished program & implemented way to install via cmake --install

This commit is contained in:
theskywinds 2025-06-27 12:38:11 +02:00
parent f9d9eae92f
commit f21cd08782
4 changed files with 56 additions and 18 deletions

View File

@ -25,4 +25,8 @@ target_compile_options(${PROJECT_NAME} PRIVATE
-Wconversion # Warn on type conversions that may lose data -Wconversion # Warn on type conversions that may lose data
-Wsign-conversion # Warn on sign conversions -Wsign-conversion # Warn on sign conversions
-Wnull-dereference # Warn about null pointer dereference -Wnull-dereference # Warn about null pointer dereference
)
install(TARGETS ${CMAKE_PROJECT_NAME}
RUNTIME DESTINATION bin
) )

View File

@ -8,6 +8,6 @@
#include <ctime> #include <ctime>
namespace distime { namespace distime {
void parseTimeInput(std::tm &resultTime, const std::tm &currentTime); void parseTimeInput(std::tm &resultTime, const std::tm &currentTime, bool shortTime);
} }
#endif //DISTIME_H #endif //DISTIME_H

View File

@ -5,17 +5,21 @@
#include <iostream> #include <iostream>
namespace distime { namespace distime {
void parseTimeInput(std::tm &resultTime, const std::tm &currentTime) { void parseTimeInput(std::tm &resultTime, const std::tm &currentTime, bool shortTime) {
std::string timeInput{}; std::string timeInput{};
bool continueLoop{true}; bool continueLoop{true};
while (continueLoop) { while (continueLoop) {
// Unique formatting of 'cout' should make the output easier to predict // Unique formatting of 'cout' should make the output easier to predict
std::cout std::cout
//blank << "\nCurrent time: " << std::put_time(&currentTime, "%d-%m-%Y %H:%M\n");
<< "\nCurrent time: " << std::put_time(&currentTime, "%d-%m-%Y %H:%M\n")
//blank if (shortTime) {
<< "Day (dd) Month (mm) Year(yyyy) 24 Hour (HH) Minute (MM)\n" 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); std::getline(std::cin, timeInput);
@ -26,12 +30,21 @@ namespace distime {
int spaceCheck = static_cast<int>(std::ranges::count(timeInput.cbegin(), timeInput.cend(), ' ')); int spaceCheck = static_cast<int>(std::ranges::count(timeInput.cbegin(), timeInput.cend(), ' '));
if (spaceCheck != 4) { if (spaceCheck != 4 && shortTime == false) {
if (spaceCheck < 4) if (spaceCheck < 4)
std::cout << "Too few spaces\n"; std::cout << "Too few spaces\n";
else if (spaceCheck > 4) else if (spaceCheck > 4)
std::cout << "Too many spaces\n"; 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::cout << "Press enter to continue...";
std::cin.get(); std::cin.get();
continue; continue;
@ -42,9 +55,21 @@ namespace distime {
std::istringstream timeStream {timeInput}; 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 // Assign the values to resultTime
resultTime.tm_mday = day; resultTime.tm_mday = day;

View File

@ -14,7 +14,7 @@ void validateTimeFormatSelection(char timeFormat) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
try {
CLI::App app{}; CLI::App app{};
app.usage("USAGE: distime [OPTIONS]"); app.usage("USAGE: distime [OPTIONS]");
@ -23,8 +23,14 @@ int main(int argc, char **argv) {
->multi_option_policy (CLI::MultiOptionPolicy::Throw) ->multi_option_policy (CLI::MultiOptionPolicy::Throw)
->default_val('f'); ->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 { [] () { auto formatHelp { [] () {
std::cout std::cout
<< "FORMATS:\n"
<< "F - Long format [Friday, June 27, 2025 at 9:00]\n" << "F - Long format [Friday, June 27, 2025 at 9:00]\n"
<< "f - Short Long Format [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" << "D - Short Format [June 27, 2025]\n"
@ -40,20 +46,23 @@ int main(int argc, char **argv) {
CLI11_PARSE(app, argc, 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); std::time_t result = std::time(nullptr);
const std::tm currentTime = *std::localtime(&result); const std::tm currentTime = *std::localtime(&result);
[[maybe_unused]] std::tm resultTime = currentTime; [[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 << "Resulting time: " << std::put_time(&resultTime, "%d-%m-%Y %H:%M\n");
std::cout << "Time stamp: <t:" << std::mktime(&resultTime) << ':' << timeFormat << '>'; std::cout << "Time stamp: <t:" << std::mktime(&resultTime) << ':' << timeFormat << '>';
return EXIT_SUCCESS; return EXIT_SUCCESS;
}
catch (const std::invalid_argument &e) {
std::cerr << "Error: " << e.what();
}
} }