From 74ff7c3a783e3c111a48715108a5b6a3d25e49b8 Mon Sep 17 00:00:00 2001 From: SanJacobs Date: Sun, 17 Apr 2022 23:23:39 +0200 Subject: Refactor and bugfix --- src/time.cpp | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) (limited to 'src/time.cpp') diff --git a/src/time.cpp b/src/time.cpp index b4bfa1e..384eb83 100755 --- a/src/time.cpp +++ b/src/time.cpp @@ -7,44 +7,23 @@ // --- OPERATOR OVERLOADS --- // +long sortable_time(const moment input_moment) { + return stol(std::to_string(input_moment.year)+ + padint(input_moment.month,2)+ + padint(input_moment.day,2)+ + padint(input_moment.hours,2)+ + padint(input_moment.minutes,2)); +} + 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); + return bool(sortable_time(*this) < sortable_time(other)); } + 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); + return bool(sortable_time(*this) > sortable_time(other)); } -bool moment::operator=(const moment& other) const { + +bool moment::operator==(const moment& other) const { return bool(year==other.year && month==other.month && day==other.day && @@ -63,7 +42,7 @@ std::string padint(const int input, const int minimum_signs) { return output.str(); } -timeblock timesplit(timeblock &input_block, const 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. @@ -77,7 +56,7 @@ timeblock timesplit(timeblock &input_block, const moment splitpoint) { return output; } -void wind(moment &input_moment, const int minutes, const int hours, const int days) { +void wind(moment& input_moment, const int minutes, const int hours, const int days) { // Adding minutes input_moment.minutes += minutes; @@ -142,6 +121,7 @@ std::string timeprint(timeblock input_timeblock) { return output; } + int days_in(int month, int year) { // Kind of a stupid and slow way to do this // But it's nice to have it as a function -- cgit v1.2.1