Bunch of refactoring & small changes. Implemented epoch time as an output & fully implemented time input system.
This commit is contained in:
parent
008d044f73
commit
91d5b4d0d1
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
13
include/distime.h
Normal file
13
include/distime.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
//
|
||||
// Created by theskywinds on 25-06-25.
|
||||
//
|
||||
|
||||
#ifndef DISTIME_H
|
||||
#define DISTIME_H
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace distime {
|
||||
void parseTimeInput(std::tm &resultTime, const std::tm ¤tTime);
|
||||
}
|
||||
#endif //DISTIME_H
|
||||
52
src/distime.cpp
Normal file
52
src/distime.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "../include/distime.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
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<int>(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;
|
||||
}
|
||||
}
|
||||
42
src/main.cpp
42
src/main.cpp
|
|
@ -1,18 +1,20 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#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<MyFormatter>();
|
||||
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;
|
||||
std::cout << std::put_time(&resultTime, "%d-%m-%Y %H:%M\n");
|
||||
std::cout << "Time since epoch: " << std::mktime(&resultTime);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user