From f2fb044281983cd6806f84dda56ebc26655b6ee1 Mon Sep 17 00:00:00 2001 From: San Jacobs Date: Sun, 3 Apr 2022 14:40:19 +0200 Subject: Progress made on C++ version --- src/main.cpp | 24 +++++++++++--- src/time.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/time.h | 28 +++++++++++++++++ 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 src/time.cpp create mode 100644 src/time.h diff --git a/src/main.cpp b/src/main.cpp index 6a178ca..910c369 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,16 +16,30 @@ You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/ */ -/* TODO: Make a system that effeciently stores a range of time, and lets you split it up neatly */ -/* TODO: Make the system that determines the price of each of those slices of time */ +// TODO: Make a system that effeciently stores a range of time, and lets you split it up neatly +// The slicing function could use a pointer to output the posterior half of the time range into +// The slicing process should figure out how many slices will need to be made before doing the slices, so a correctly sized array can be allocated on the stack instead of using a vec on the heap + +// TODO: Make the system that determines the price of each of those slices of time + +// TODO: Test the boost time date system, see if it accounts for leap years and DST. #include -#include +#include "time.h" +//#include +//#include int main() { - tm test_moment_one{0, 30, 14, 27, 11, 2010}; - std::cout << "Testing 123" << std::endl; + //using boost::posix_time::to_simple_string; + //tm test_moment_one{0, 30, 14, 27, 11, 2010}; + moment calltime{30, 8, 27, 11, 2010}; + moment wraptime{30, 16, 27, 11, 2010}; + timeblock workday{calltime, wraptime}; + + + std::cout << "Testing 123\n"; + std::cout << timeprint(workday.start) << std::endl; return 0; } diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..3b65ccc --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,101 @@ +#include "time.h" + +// 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) { + timeblock output{splitpoint, input_block.end}; + input_block.end = splitpoint; + return output; +} + +void wind(moment &input_moment, int minutes, int hours, int days) { + + // Adding minutes + input_moment.minutes += minutes; + while(input_moment.minutes > 59) { + input_moment.minutes -= 60; + input_moment.hours++; + } + while(input_moment.minutes < 0) { + input_moment.minutes += 60; + input_moment.hours--; + } + + // Adding hours + input_moment.hours += hours; + while(input_moment.hours > 23) { + input_moment.hours -= 24; + input_moment.day++; + } + while(input_moment.hours < 0) { + input_moment.hours += 24; + input_moment.day--; + } + + // Adding days + input_moment.day += days; + // FIXME: Months don't roll over.'The -3th of January will cause problems. + // ^^^^^^^ + // Fixing this will just take some simple modulo math. + int current_month_length = days_in(input_moment.month, input_moment.year); + while(input_moment.day > current_month_length) { + input_moment.day -= current_month_length; + input_moment.month++; + current_month_length = days_in(input_moment.month, input_moment.year); + while(input_moment.day < 1) { + input_moment.day += days_in(((input_moment.month-1)%12)+1); // FIXME, this is absolutely retarded at the moment + input_moment.month++; + current_month_length = days_in(input_moment.month, input_moment.year); + } +} + +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) + " " + + to_string(input_moment.year) + "-" + + to_string(input_moment.month) + "-" + + to_string(input_moment.day); + 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 + // because of the leap year arithmatic + switch (month) { + case 1: + return 31; + case 2: + if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){ + return 29; + } + return 28; + case 3: + return 31; + case 4: + return 30; + case 5: + return 31; + case 6: + return 30; + case 7: + return 31; + case 8: + return 31; + case 9: + return 30; + case 10: + return 31; + case 11: + return 30; + case 12: + return 31; + } + std::cout << "Something just went very wrong. You found month #" << std::to_string(month) << '\n'; + return 5; +} diff --git a/src/time.h b/src/time.h new file mode 100644 index 0000000..5087108 --- /dev/null +++ b/src/time.h @@ -0,0 +1,28 @@ +#pragma once +#include + +struct moment{ + signed int minutes; + signed int hours; // 24-hour format. 23:59 progresses to 00:00 + signed int day; + signed int month; + signed int year; +}; + +struct timeblock{ + moment start; + moment end; +}; + +class day{ + +}; + +timeblock timesplit(timeblock &input_block, moment splitpoint); + +void wind(moment &input_moment, int minutes, int hours, int days); + +int days_in(int month, int year); + +std::string timeprint(moment input_moment); + -- cgit v1.2.1