From 91d5b4d0d1af97489884a8285e4e55d28a4ff323 Mon Sep 17 00:00:00 2001 From: theskywinds Date: Wed, 25 Jun 2025 12:40:42 +0200 Subject: [PATCH] Bunch of refactoring & small changes. Implemented epoch time as an output & fully implemented time input system. --- CMakeLists.txt | 1 + README.md | 3 ++- include/distime.h | 13 ++++++++++++ src/distime.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 44 +++++++++++++++++---------------------- 5 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 include/distime.h create mode 100644 src/distime.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e2e30a2..757ab9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ set(CMAKE_CXX_STANDARD 20) add_executable(${CMAKE_PROJECT_NAME} src/main.cpp + src/distime.cpp ) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE lib) diff --git a/README.md b/README.md index d7c0e71..ffd44e9 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ Mainly made in mind with Linux (Although it will in the future work on Windows t The program should run purely in the terminal, working on Windows and Linux. It should take a couple of commandline arguments based on -1. What time format (By default 'f'). +1. What time format (By default 'f') 2. Short format (HH MM SS) or long format (DD MM YY HH MM SS) +3. A help command for giving information on the time stamps It start the output as: - Current time diff --git a/include/distime.h b/include/distime.h new file mode 100644 index 0000000..4c5bfb2 --- /dev/null +++ b/include/distime.h @@ -0,0 +1,13 @@ +// +// Created by theskywinds on 25-06-25. +// + +#ifndef DISTIME_H +#define DISTIME_H + +#include + +namespace distime { + void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime); +} +#endif //DISTIME_H diff --git a/src/distime.cpp b/src/distime.cpp new file mode 100644 index 0000000..33f9bd3 --- /dev/null +++ b/src/distime.cpp @@ -0,0 +1,52 @@ +#include "../include/distime.h" + +#include +#include +#include + +namespace distime { + void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime) { + 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" + << ">: "; + + std::getline(std::cin, timeInput); + + int spaceCheck = static_cast(std::ranges::count(timeInput.cbegin(), timeInput.cend(), ' ')); + + if (spaceCheck != 4) { + 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 << "Press enter to continue..."; + std::cin.get(); + continue; + } + continueLoop = false; + } + + + std::istringstream timeStream {timeInput}; + + int day, month, year, hour, minute; + + timeStream >> day >> month >> year >> hour >> minute; + + // Assign the values to resultTime + resultTime.tm_mday = day; + resultTime.tm_mon = month - 1; + resultTime.tm_year = year - 1900; + resultTime.tm_hour = hour; + resultTime.tm_min = minute; + resultTime.tm_sec = 0; + } +} diff --git a/src/main.cpp b/src/main.cpp index 1e54f0b..d1a3c81 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,20 @@ #include +#include #include "CLI11.hpp" +#include "../include/distime.h" -class MyFormatter : public CLI::Formatter { -public: - MyFormatter() : Formatter() {} - std::string make_usage([[maybe_unused]] const CLI::App *app, [[maybe_unused]] std::string name) const override { - return "USAGE: " + name + " [OPTIONS]"; +void validateTimeFormatSelection(char timeFormat) { + if (timeFormat != 'F' && timeFormat != 'f' && + timeFormat != 'D' && timeFormat != 'd' && + timeFormat != 'T' && timeFormat != 't' && + timeFormat != 'R') { + throw std::invalid_argument("Invalid time format selected"); } -}; +} int main(int argc, char **argv) { CLI::App app{}; - auto fmt = std::make_shared(); - app.formatter(fmt); + app.usage("USAGE: distime [OPTIONS]"); char timeFormat {}; @@ -22,24 +24,16 @@ int main(int argc, char **argv) { CLI11_PARSE(app, argc, argv); - try { - if (timeFormat != 'F' && timeFormat != 'f' && - timeFormat != 'D' && timeFormat != 'd' && - timeFormat != 'T' && timeFormat != 't' && - timeFormat != 'R') { - throw std::invalid_argument("Invalid format selected"); - } - } catch (const std::invalid_argument& e) { - std::cerr << "Error: " << e.what() << std::endl; - std::exit(EXIT_FAILURE); - } + validateTimeFormatSelection(timeFormat); std::time_t result = std::time(nullptr); - std::tm currentTime = *std::localtime(&result); - std::tm resultTime = currentTime; + const std::tm currentTime = *std::localtime(&result); + [[maybe_unused]] std::tm resultTime = currentTime; - std::cout << std::asctime(&resultTime); + distime::parseTimeInput(resultTime, currentTime); - std::cout << "Parameter value: " << timeFormat << std::endl; - return 0; -} \ No newline at end of file + std::cout << std::put_time(&resultTime, "%d-%m-%Y %H:%M\n"); + std::cout << "Time since epoch: " << std::mktime(&resultTime); + + return EXIT_SUCCESS; +}