From 8ff17a99948f913b7741e566b5339db619c1f1eb Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 14 Feb 2026 09:53:53 +0100 Subject: [PATCH] change get saldo seconds from string --- src/time_format.c | 12 +++++++----- src/time_format.h | 2 +- src/xml.c | 12 ++---------- tests/time_format_tests.c | 6 ++---- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/time_format.c b/src/time_format.c index 7b1cf2e..24d6917 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -53,12 +53,12 @@ void get_time_str_overtime(time_t timediff, char* timestr) sprintf(timestr, "-%02d:%02d", hour, minutes); } -void get_seconds_from_string(const char* timestr, time_t* seconds) +time_t get_seconds_from_string(const char* timestr) { char* str = malloc(sizeof(char) * 7); if (str == NULL) - return; + return 0; memset(str, 0, 7); strncpy(str, timestr, 7); @@ -69,17 +69,19 @@ void get_seconds_from_string(const char* timestr, time_t* seconds) minus = true; str = strtok(str, ":"); - *seconds = atol(str) * ONE_HOUR; + time_t seconds = atol(str) * ONE_HOUR; str = strtok(NULL, ":"); if (minus) - *seconds += atol(str) * 60 * -1; + seconds += atol(str) * 60 * -1; else - *seconds += atol(str) * 60; + seconds += atol(str) * 60; free(first_str_pos); first_str_pos = NULL; + + return seconds; } char* get_weekday_str(uint8_t wday) diff --git a/src/time_format.h b/src/time_format.h index dbdffdb..35f26ba 100644 --- a/src/time_format.h +++ b/src/time_format.h @@ -6,7 +6,7 @@ void get_time_str(time_t timediff, char* timestr); void get_time_str_overtime(time_t timediff, char* timestr); -void get_seconds_from_string(const char* timestr, time_t* seconds); +time_t get_seconds_from_string(const char* timestr); char* get_weekday_str(uint8_t wday); char* get_short_weekday(uint8_t wday); uint16_t get_year_from_str(const char* date); diff --git a/src/xml.c b/src/xml.c index c6b9d48..989cf99 100644 --- a/src/xml.c +++ b/src/xml.c @@ -355,12 +355,12 @@ void add_entry(const char* date, read_stdin_into_memory(fileContent); char* saldostr = malloc(sizeof(char) * MAX_STRING_LENGTH); - if (saldostr == NULL) return; memset(saldostr, 0, MAX_STRING_LENGTH); read_last_saldo(fileContent, saldostr); + const time_t saldo = get_seconds_from_string(saldostr); xmlChar* xmlElemContent = NULL; xmlCharEncodingHandlerPtr encodingHandler = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); @@ -392,13 +392,7 @@ void add_entry(const char* date, } } - time_t* saldo = malloc(sizeof(time_t)); - - if (saldo == NULL) - return; - - get_seconds_from_string(saldostr, saldo); - write_entry_node(xmlwriter, date, begin_hour, begin_min, end_hour, end_min, *saldo); + write_entry_node(xmlwriter, date, begin_hour, begin_min, end_hour, end_min, saldo); xmlTextWriterEndElement(xmlwriter); xmlTextWriterEndDocument(xmlwriter); @@ -419,8 +413,6 @@ void add_entry(const char* date, fileContent = NULL; free(xmlElemContent); xmlElemContent = NULL; - free(saldo); - saldo = NULL; free(saldostr); saldostr = NULL; } diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 6da35c2..e34e12d 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -286,9 +286,8 @@ void get_short_unknown() void get_seconds_from_str() { const char* timestr = "04:15"; - time_t seconds = 0; - get_seconds_from_string(timestr, &seconds); + const time_t seconds = get_seconds_from_string(timestr); assert_int_equal(seconds, 15300); } @@ -296,9 +295,8 @@ void get_seconds_from_str() void get_seconds_from_str_minus() { const char* timestr = "-04:15"; - time_t seconds = 0; - get_seconds_from_string(timestr, &seconds); + const time_t seconds = get_seconds_from_string(timestr); assert_int_equal(seconds, -15300); } -- 2.47.3