From b99fe6cb748be0cf018540180bf19edd1bb1a698 Mon Sep 17 00:00:00 2001 From: SanJacobs Date: Fri, 15 Apr 2022 20:29:24 +0200 Subject: Added padding 0s and made time-system way comfier --- src/time.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 9 deletions(-) (limited to 'src/time.cpp') diff --git a/src/time.cpp b/src/time.cpp index 1262d57..8f12d3d 100755 --- a/src/time.cpp +++ b/src/time.cpp @@ -2,19 +2,74 @@ // Look. listen here. There's no way I'm going to start taking DST into account. // I have to draw the line somewhere, and frankly, once you start doing "Change an hour on the 4th moon of the 2nd week of March in France, but only if the tulips haven't sprung... etc... etc.." I'm out. -// Just do it on a set day, God dammit. -timeblock timesplit(timeblock &input_block, moment splitpoint) { +// Operator overloads: +bool moment::operator<(const moment& other) const{ + // Converts moments to strings and casts them as ints to compare + using namespace std; + string left_moment_string{ + to_string(year)+ + padint(month,2)+ + padint(day,2)+ + padint(hours,2)+ + padint(minutes,2)}; + string right_moment_string{ + to_string(other.year)+ + padint(other.month,2)+ + padint(other.day,2)+ + padint(other.hours,2)+ + padint(other.minutes,2)}; + long left_int = stol(left_moment_string); + long right_int = stol(right_moment_string); + return bool(left_int < right_int); +} +bool moment::operator>(const moment& other) const{ + using namespace std; + string left_moment_string{ + to_string(year)+ + padint(month,2)+ + padint(day,2)+ + padint(hours,2)+ + padint(minutes,2)}; + string right_moment_string{ + to_string(other.year)+ + padint(other.month,2)+ + padint(other.day,2)+ + padint(other.hours,2)+ + padint(other.minutes,2)}; + long left_int = stol(left_moment_string); + long right_int = stol(right_moment_string); + return bool(left_int > right_int); +} +bool moment::operator=(const moment& other) const { + return bool(year==other.year && + month==other.month && + day==other.day && + hours==other.hours && + minutes==other.minutes); +} + +std::string padint(const int input, const int minimum_signs) { + std::ostringstream output; + output << std::internal << std::setfill('0') << std::setw(minimum_signs) << input; + return output.str(); +} + +timeblock timesplit(timeblock &input_block, const moment splitpoint) { // Splits a timeblock at splitpoint. // It changes the input_block to end at splitpoint, and returns a new timeblock // that lasts from splitpoint to where the input_block used to end. - // TODO: Throw error if splitpoint is outside timeblock. + if(splitpoint < input_block.start || splitpoint > input_block.end) { + std::cerr << "ERROR: Splitpoint outside of timeblock!\n"; + std::cerr << "Timeblock: " << timeprint(input_block) << std::endl; + std::cerr << "Splitpoint: " << timeprint(splitpoint) << std::endl; + } timeblock output{splitpoint, input_block.end}; input_block.end = splitpoint; return output; } -void wind(moment &input_moment, int minutes, int hours, int days) { +void wind(moment &input_moment, const int minutes, const int hours, const int days) { // Adding minutes input_moment.minutes += minutes; @@ -65,13 +120,12 @@ void wind(moment &input_moment, int minutes, int hours, int days) { std::string timeprint(moment input_moment) { using namespace std; - // TODO: Add leading zeroes to hours and minutes string output = - to_string(input_moment.hours) + ":" - + to_string(input_moment.minutes) + " " + padint(input_moment.hours, 2) + ":" + + padint(input_moment.minutes, 2) + " " + to_string(input_moment.year) + "-" - + to_string(input_moment.month) + "-" - + to_string(input_moment.day); + + padint(input_moment.month, 2) + "-" + + padint(input_moment.day, 2); return output; } -- cgit v1.2.1