aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2025-11-16 10:40:30 +0100
committerTristan Riehs <tristan.riehs@inria.fr>2025-11-16 10:40:30 +0100
commitd5e54b0b9809186ff319ecf62ac94707243e0514 (patch)
tree35e271555e7815026d54fd63a706677ce477adb4
parent3c51013fd46ad10b381038932d282cc2bc45df72 (diff)
Update date management
Use integers.
-rw-r--r--src/main.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/src/main.c b/src/main.c
index a2ecb4e..bdbecbc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -168,7 +168,7 @@ static void ftag_add_one_file(sqlite3 *db,
char *full_name = NULL;
char *canonical_name = NULL;
char *description = NULL;
- char *date = NULL;
+ time_t date = time(NULL);
size_t line_len = 0;
ssize_t read_len;
@@ -221,27 +221,32 @@ static void ftag_add_one_file(sqlite3 *db,
description[read_len-1] = '\0';
assert_no_single_quote(description);
- line_len = 32;
- date = malloc(line_len);
- time_t now = time(NULL);
- struct tm *now_tm = localtime(&now);
- strftime(date, line_len-1, "%Y-%m-%d", now_tm);
+ line_len = 64;
+ char *date_str = malloc(line_len);
+ struct tm *now_tm = localtime(&date);
+ strftime(date_str, line_len-1, "%Y-%m-%d", now_tm);
printf("Enter the date in the format YYYY-MM-DD. If no input is\n"
- "given, date will be \"%s\".\n", date);
- read_len = getline(&date, &line_len, stdin);
+ "given, date will be \"%s\".\n", date_str);
+ read_len = getline(&date_str, &line_len, stdin);
if (read_len == -1) {
perror("getline");
exit(EXIT_FAILURE);
}
- if (date[0] == '\n')
- strftime(date, line_len-1, "%Y-%m-%d", now_tm);
- else
- date[read_len-1] = '\0';
+ 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);
+ date = mktime(&tm);
+ if (rc != 3) {
+ perror("sccanf");
+ exit(EXIT_FAILURE);
+ }
+ }
memset(sql, 0, sizeof(sql));
snprintf(sql, sizeof(sql)-1,
- "INSERT INTO files VALUES(%d, '%s', '%s', '%s', '%s')",
- *next_id, canonical_name, full_name, description, date);
+ "INSERT INTO files VALUES(%d, '%s', '%s', '%s', %ld)",
+ next_id, canonical_name, full_name, description, date);
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
sqlite3_check(rc, db);