From afdc8945685cfcefce653bcd5bb0c82166c5a7c3 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 22 Jun 2024 10:03:57 +0200 Subject: [PATCH] add method for overtime hours --- src/time_format.c | 16 ++++++++++++++ src/time_format.h | 3 ++- tests/time_format_tests.c | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/time_format.c b/src/time_format.c index e8eccc0..1a6d060 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -20,6 +20,22 @@ void get_time_str(time_t timediff, char* timestr) sprintf(timestr, "%02d:%02d", hour, minutes); } +void get_time_str_overtime(time_t timediff, char* timestr) +{ + int hour = timediff / ONE_HOUR; + int rest_minutes = timediff % ONE_HOUR; + int minutes = rest_minutes / MINUTE; + + if (timediff < 0) { + hour *= -1; + minutes *= -1; + sprintf(timestr, "%02d:%02d", hour, minutes); + return; + } + + sprintf(timestr, "-%02d:%02d", hour, minutes); +} + void get_weekday(int wday, char* weekday) { switch (wday) { diff --git a/src/time_format.h b/src/time_format.h index d4e53ec..8dcaa02 100644 --- a/src/time_format.h +++ b/src/time_format.h @@ -2,7 +2,8 @@ #define TIME_FORMAT_H #include -void get_time_str(time_t time, char* timestr); +void get_time_str(time_t timediff, char* timestr); +void get_time_str_overtime(time_t timediff, char* timestr); void get_weekday(int wday, char* weekday); void get_short_weekday(int wday, char* weekday); diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 1dee4cf..17ef589 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -49,6 +49,48 @@ void get_minus_five_minutes() timestring = NULL; } +void get_zero_hour_and_fifteen_minutes_overtime() +{ + time_t time = 900; + char* timestring = malloc(sizeof(char) * 7); + memset(timestring, 0, 7); + + get_time_str_overtime(time, timestring); + + assert_string_equal(timestring, "-00:15"); + + free(timestring); + timestring = NULL; +} + +void get_five_hour_fourty_five_overtime() +{ + time_t time = 20700; + char* timestring = malloc(sizeof(char) * 7); + memset(timestring, 0, 7); + + get_time_str_overtime(time, timestring); + + assert_string_equal(timestring, "-05:45"); + + free(timestring); + timestring = NULL; +} + +void get_minus_five_minutes_overtime() +{ + time_t time = -300; + char* timestring = malloc(sizeof(char) * 7); + memset(timestring, 0, 7); + + get_time_str_overtime(time, timestring); + + assert_string_equal(timestring, "00:05"); + + free(timestring); + timestring = NULL; +} + void get_thursday() { char* weekday = malloc(sizeof(char) * 11); @@ -230,6 +272,9 @@ int main() cmocka_unit_test(get_zero_hour_and_fifteen_minutes), cmocka_unit_test(get_five_hour_fourty_five), cmocka_unit_test(get_minus_five_minutes), + cmocka_unit_test(get_zero_hour_and_fifteen_minutes_overtime), + cmocka_unit_test(get_five_hour_fourty_five_overtime), + cmocka_unit_test(get_minus_five_minutes_overtime), cmocka_unit_test(get_thursday), cmocka_unit_test(get_friday), cmocka_unit_test(get_saturday), -- 2.39.5