]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
add valid date tests
authorBastian Dehn <hhaalo@arcor.de>
Tue, 11 Nov 2025 16:54:52 +0000 (17:54 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 11 Nov 2025 16:54:52 +0000 (17:54 +0100)
src/time_format.c
src/time_format.h
tests/time_format_tests.c

index 4014e0170c5837d112a1e26c492ab72abd5393f0..e79911dd216e0228cca7abbca64764767ef686d6 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <regex.h>
 #include "time_format.h"
 
 #define ONE_HOUR 3600
@@ -9,6 +10,8 @@
 #define SHORT_WEEKDAY_STR_LEN 3
 #define MAX_TIME_STR_LENGTH 7
 
+#define DATE_REGEX "[0-9]\\{4\\}-\\([0][1-9]\\|1[0-2]\\)-\\([0-2][1-9]\\|[1-3]0\\|3[01]\\)"
+
 void get_time_str(time_t timediff, char* timestr)
 {
        memset(timestr, 0, MAX_TIME_STR_LENGTH);
@@ -182,4 +185,31 @@ int get_day_from_str(const char* date)
        free(str_first_pos);
        str_first_pos = NULL;
        return value;
+}
+
+bool validate_datestring(const char* date)
+{
+       regex_t* date_regex = malloc(sizeof(regex_t));
+       if (date_regex == NULL)
+               return false;
+
+       int comp_success = regcomp(date_regex, DATE_REGEX, REG_NOSUB);
+       if (comp_success != 0) {
+               free(date_regex);
+               return false;
+       }
+
+       int match = regexec(date_regex, date, 0, NULL, 0);
+
+       regfree(date_regex);
+       free(date_regex);
+       date_regex = NULL;
+
+       if (match == REG_NOMATCH)
+               return false;
+
+       if (match == 0)
+               return true;
+
+       return false;
 }
\ No newline at end of file
index 669a94cf31b3628c504eb08e90241a2a889cabd9..712cfaf1f36a4e1127b7c9b8b9ed550b1924a948 100644 (file)
@@ -1,5 +1,6 @@
 #ifndef TIME_FORMAT_H
 #define TIME_FORMAT_H
+#include <stdbool.h>
 #include <time.h>
 
 void get_time_str(time_t timediff, char* timestr);
@@ -10,5 +11,6 @@ void get_short_weekday(int wday, char* weekday);
 int get_year_from_str(const char* date);
 int get_month_from_str(const char* date);
 int get_day_from_str(const char* date);
+bool validate_datestring(const char* date);
 
 #endif
\ No newline at end of file
index 1b1d731c0b6875c94cf2c8ef0cf06d9d47fbc9ac..6014dfbc3ac5eb866b2ea736b74362cb205dcf3a 100644 (file)
@@ -328,6 +328,23 @@ void get_dat_from_date()
        assert_int_equal(result, 24);
 }
 
+void validate_valid_date_string(const char* date)
+{
+       bool valid = validate_datestring(date);
+
+       assert_true(valid);
+}
+
+void validate_valid_date_string_tests()
+{
+       validate_valid_date_string("2025-01-01");
+       validate_valid_date_string("2025-01-31");
+       validate_valid_date_string("2025-10-01");
+       validate_valid_date_string("2025-10-31");
+       validate_valid_date_string("2025-12-01");
+       validate_valid_date_string("2025-12-31");
+}
+
 int main()
 {
        const struct CMUnitTest tests[] = {
@@ -356,7 +373,8 @@ int main()
                cmocka_unit_test(get_seconds_from_str_minus),
                cmocka_unit_test(get_year_from_date),
                cmocka_unit_test(get_month_from_date),
-               cmocka_unit_test(get_dat_from_date)
+               cmocka_unit_test(get_dat_from_date),
+               cmocka_unit_test(validate_valid_date_string_tests)
        };
 
        return cmocka_run_group_tests(tests, NULL, NULL);