]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
change get saldo seconds from string
authorBastian Dehn <hhaalo@arcor.de>
Sat, 14 Feb 2026 08:53:53 +0000 (09:53 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 14 Feb 2026 08:53:53 +0000 (09:53 +0100)
src/time_format.c
src/time_format.h
src/xml.c
tests/time_format_tests.c

index 7b1cf2ea1422e3437262259f34c55692ad3b955c..24d691736282988f04d76b1344650c397625200f 100644 (file)
@@ -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)
index dbdffdbe3f4fe44a22d6a5e1ee92330b35bd511d..35f26ba60657dff1a40d5027111e7adcd3ae977e 100644 (file)
@@ -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);
index c6b9d48871a5396759fbb015fd8ac3cdb7145d9b..989cf9985cbd62fb9ba718f33460dbfa41cd6100 100644 (file)
--- 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;
 }
index 6da35c252603bd8f6277e430aa7559a682089d34..e34e12d8c4bcb84a4ed376a39db6e73fe29b2698 100644 (file)
@@ -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);
 }