diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/time.cpp | 72 | ||||
-rwxr-xr-x | src/time.h | 15 |
2 files changed, 73 insertions, 14 deletions
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; } @@ -1,5 +1,8 @@ #pragma once #include <iostream> +#include <sstream> +#include <ios> +#include <iomanip> struct moment{ signed int minutes; @@ -7,6 +10,10 @@ struct moment{ signed int day; signed int month; signed int year; + + bool operator<(const moment& other) const; + bool operator>(const moment& other) const; + bool operator=(const moment& other) const; }; struct timeblock{ @@ -14,16 +21,14 @@ struct timeblock{ moment end; }; -class day{ - -}; +std::string padint(const int input, const int minimum_signs); -timeblock timesplit(timeblock &input_block, moment splitpoint); +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. -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); int days_in(int month, int year); |