From 8ffe93fa5acbe9b92c2daa204943312c81edd628 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Mon, 17 Nov 2025 09:32:27 +0100 Subject: Factorize date parsing --- src/main.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 29f98f3..af74c2c 100644 --- a/src/main.c +++ b/src/main.c @@ -76,6 +76,24 @@ static void assert_no_single_quote(const char *str) assert(strchr(str, ';') == NULL); } +static time_t time_from_str(const char *str) +{ + int rc; + struct tm tm; + memset(&tm, 0, sizeof(tm)); + rc = sscanf(str, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday); + if (rc <= 0) { + perror("sccanf"); + exit(EXIT_FAILURE); + } + printf("%s: debug: year is %d\n", __func__, tm.tm_year); + printf("%s: debug: month is %d\n", __func__, tm.tm_mon); + printf("%s: debug: day is %d\n", __func__, tm.tm_mday); + tm.tm_year -= 1900; + tm.tm_mon -= 1; + return mktime(&tm); +} + /* Write an id to *_ID, used by table_next_id. Write -1 if no id is found. */ static int table_next_id_callback(void *_id, int, char **cols, char **) { @@ -236,17 +254,7 @@ static void ftag_add_one_file(sqlite3 *db, exit(EXIT_FAILURE); } if (read_len > 1) { - struct tm tm; - memset(&tm, 0, sizeof(tm)); - rc = sscanf(date_str, "%d-%d-%d\n", - &tm.tm_year, &tm.tm_mon, &tm.tm_mday); - tm.tm_year -= 1900; - tm.tm_mon -= 1; - date = mktime(&tm); - if (rc <= 0) { - perror("sccanf"); - exit(EXIT_FAILURE); - } + date = time_from_str(date_str); } memset(sql, 0, sizeof(sql)); @@ -450,18 +458,18 @@ static void ftag_query(int argc, char **argv) ftag_query_usage(); exit(EXIT_FAILURE); } - char *after_date __attribute__((unused)); - char *before_date __attribute__((unused)); + time_t after_date __attribute__((unused)) = 0; + time_t before_date __attribute__((unused)) = 0; char **tags = NULL; int tag_count = 0; while (argc > 0) { if (strcmp(argv[0], "-a") == 0) { assert(argc >= 2); - after_date = argv[1]; + after_date = time_from_str(argv[1]); } else if (strcmp(argv[0], "-b") == 0) { assert(argc >= 2); - before_date = argv[1]; + before_date = time_from_str(argv[1]); } else if (strcmp(argv[0], "-t") == 0) { assert(argc >= 2); -- cgit v1.2.3