]> git.ipfire.org Git - pakfire.git/commitdiff
string: Refactor strftime() functions
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 19:17:50 +0000 (19:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 19:17:50 +0000 (19:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/string.h
src/libpakfire/package.c
src/libpakfire/string.c

index aa94eddaa57df767c56e927742fb0a116a456729..6c34fd023eeb381044d57bf2a3c67d747665eb13 100644 (file)
@@ -53,12 +53,18 @@ char* pakfire_string_join(char** list, const char* delim);
        __pakfire_format_size(dst, sizeof(dst) - 1, value)
 int __pakfire_format_size(char* dst, size_t length, double value);
 int pakfire_format_speed(char* dst, size_t length, double value);
+
+#define pakfire_strftime(buffer, format, t) \
+       __pakfire_strftime(buffer, sizeof(buffer), format, t)
+int __pakfire_strftime(char* buffer, const size_t length,
+       const char* format, const time_t t) __attribute__((format(strftime, 3, 0)));
+
 char* pakfire_format_date(time_t t);
 
-#define pakfire_strftime_now(dest, format) \
-       __pakfire_strftime_now(dest, sizeof(dest) - 1, format)
-int __pakfire_strftime_now(char* dest, size_t length, const char* format)
-       __attribute__((format(strftime, 3, 0)));;
+#define pakfire_strftime_now(buffer, format) \
+       __pakfire_strftime_now(buffer, sizeof(dest), format)
+int __pakfire_strftime_now(char* buffer, const size_t length, const char* format)
+       __attribute__((format(strftime, 3, 0)));
 
 #endif
 
index 2671c5ebcbe430aaa69c2199e52fa4bf2f33b3c9..09cfe60a6e19f777d861fba4640f1b1080f1e33d 100644 (file)
@@ -1022,14 +1022,16 @@ static void pakfire_package_dump_add_lines(char** str, const char* key, const ch
        }
 }
 
-static void pakfire_package_dump_add_line_date(char** str, const char* key, unsigned long long date) {
-       // Convert from integer to tm struct.
-       struct tm* timer = gmtime((time_t *)&date);
+static void pakfire_package_dump_add_line_date(char** str, const char* key, time_t date) {
+       char buffer[1024];
+       int r;
 
-       char val[128];
-       strftime(val, sizeof(val) - 1, "%a, %d %b %Y %T %z", timer);
+       // Format time
+       r = pakfire_strftime(buffer, "%a, %d %b %Y %T %z", date);
+       if (r)
+               return;
 
-       pakfire_package_dump_add_line(str, key, val);
+       pakfire_package_dump_add_line(str, key, buffer);
 }
 
 static void pakfire_package_dump_add_line_size(char** str, const char* key, size_t size) {
index 2f95abd4287fb115b02591330ee0524ea75eb3e6..046c7da535f80aba5e4a3a3f45b569db45ae5589 100644 (file)
@@ -355,39 +355,36 @@ int pakfire_format_speed(char* dst, size_t length, double value) {
 #pragma GCC diagnostic pop
 }
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-static char* pakfire_strftime(const char* format, time_t t) {
-       char string[128];
+int __pakfire_strftime(char* buffer, const size_t length,
+               const char* format, const time_t t) {
        struct tm* tm = gmtime(&t);
+       int r;
 
-       strftime(string, sizeof(string) - 1, format, tm);
+       // Format time
+       r = strftime(buffer, length, format, tm);
+       if (r == 0)
+               return 1;
 
-       return strdup(string);
+       return 0;
 }
-#pragma GCC diagnostic pop
 
 char* pakfire_format_date(time_t t) {
-       return pakfire_strftime("%Y-%m-%d", t);
-}
+       char buffer[1024];
+       int r;
+
+       // Format time
+       r = pakfire_strftime(buffer, "%Y-%m-%d", t);
+       if (r)
+               return NULL;
 
-int __pakfire_strftime_now(char* dest, size_t length, const char* format) {
-       struct tm tm;
+       return strdup(buffer);
+}
 
+int __pakfire_strftime_now(char* buffer, size_t length, const char* format) {
        // Fetch the current time
        const time_t t = time(NULL);
        if (t < 0)
                return 1;
 
-       // Convert to struct tm
-       struct tm* now = gmtime_r(&t, &tm);
-       if (!now)
-               return 1;
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-       strftime(dest, length, format, now);
-#pragma GCC diagnostic pop
-
-       return 0;
+       return __pakfire_strftime(buffer, length, format, t);
 }