From 7fcf9054dbe56f1fd1e26c95343c13f17a18b30a Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Mon, 29 Jun 2020 21:04:30 -0300 Subject: [PATCH] Time calculations in its module --- c/main.c | 38 +++++++++++--------------------------- c/period.c | 33 +++++++++++++++++++++++++++++++++ c/period.h | 9 +++++++++ 3 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 c/period.c create mode 100644 c/period.h diff --git a/c/main.c b/c/main.c index 831123c..83f35b0 100644 --- a/c/main.c +++ b/c/main.c @@ -2,6 +2,7 @@ #include #include "hours.h" +#include "period.h" int main(int argc, char* argv[]) { hours *start = NULL; @@ -42,39 +43,22 @@ int main(int argc, char* argv[]) { } hours* now = hours_right_now(); - hours* so_far; - if (hours_preceed(now, lunch_return) == -1) { - so_far = hours_sub(start, now); - } else { - hours* before_lunch = hours_sub(start, lunch_break); - hours* after_lunch = hours_sub(lunch_return, now); - - so_far = hours_add(before_lunch, after_lunch); - - hours_free(before_lunch); - hours_free(after_lunch); - } - + hours* so_far = period_worked_so_far(start, lunch_break, lunch_return, now); char *display = hours_display(so_far); - printf("So far, you've worked %s\n", display); + free(display); - hours* must_work = hours_new("08:48"); - if (hours_preceed(so_far, must_work) == -1) { - hours* remaining = hours_sub(so_far, must_work); - char *still = hours_display(remaining); - printf("You still need to work %s\n", still); - free(still); - hours_free(remaining); + int extra; + hours* period = period_calc_time(so_far, &extra); + char* result_display = hours_display(period); + if (extra == 1) { + printf("You did %s of extra work\n", result_display); } else { - hours* exceeding = hours_sub(must_work, so_far); - char* extra = hours_display(exceeding); - printf("You did %s of extra work\n", extra); - free(extra); - hours_free(exceeding); + printf("You still need to work %s\n", result_display); } + free(result_display); + hours_free(period); - free(display); hours_free(so_far); hours_free(now); hours_free(start); diff --git a/c/period.c b/c/period.c new file mode 100644 index 0000000..69865ac --- /dev/null +++ b/c/period.c @@ -0,0 +1,33 @@ +#include "period.h" + +#define MUST_WORK "08:48" + +hours* period_worked_so_far(hours* start, hours* lunch_break, hours* lunch_return, hours* now) { + hours* so_far; + if (hours_preceed(now, lunch_return) == -1) { + so_far = hours_sub(start, now); + } else { + hours* before_lunch = hours_sub(start, lunch_break); + hours* after_lunch = hours_sub(lunch_return, now); + + so_far = hours_add(before_lunch, after_lunch); + + hours_free(before_lunch); + hours_free(after_lunch); + } + + return so_far; +} + +hours* period_calc_time(hours* so_far, int* extra_indicator) { + hours* must_work = hours_new(MUST_WORK); + hours* result; + if (hours_preceed(so_far, must_work) == -1) { + *extra_indicator = 0; + result = hours_sub(so_far, must_work); + } else { + *extra_indicator = 1; + result = hours_sub(must_work, so_far); + } + return result; +} diff --git a/c/period.h b/c/period.h new file mode 100644 index 0000000..f19dcad --- /dev/null +++ b/c/period.h @@ -0,0 +1,9 @@ +#ifndef PERIOD_H +#define PERIOD_H + +#include "hours.h" + +hours* period_worked_so_far(hours* start, hours* lunch_break, hours* lunch_return, hours* now); +hours* period_calc_time(hours* so_far, int* extra_indicator); + +#endif /* ifndef PERIOD_H */