#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <regex.h>
#include "time_format.h"
#define ONE_HOUR 3600
#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);
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
#ifndef TIME_FORMAT_H
#define TIME_FORMAT_H
+#include <stdbool.h>
#include <time.h>
void get_time_str(time_t timediff, char* timestr);
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
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[] = {
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);