]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add helper function for strftime to format time now
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Aug 2021 19:01:47 +0000 (19:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Aug 2021 19:01:47 +0000 (19:01 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/util.c

index a066d0c94d0e398bf7031d08f9a1555fa7263aaa..f2f7eadaf9480e6f443057bb840104f1f2fadc47 100644 (file)
@@ -51,6 +51,11 @@ int pakfire_format_size(char* dst, size_t length, double value);
 int pakfire_format_speed(char* dst, size_t length, double value);
 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)));;
+
 int pakfire_path_exists(const char* path);
 time_t pakfire_path_age(const char* path);
 
index 42ea4f7dd8a688ef4287d31ec6249bab20009104..1a424901709469fd0e48164cad15045d8ecb0998 100644 (file)
@@ -473,6 +473,27 @@ char* pakfire_format_date(time_t t) {
        return pakfire_strftime("%Y-%m-%d", t);
 }
 
+int __pakfire_strftime_now(char* dest, size_t length, const char* format) {
+       struct tm tm;
+
+       // 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;
+}
+
 int __pakfire_path_join(char* dest, size_t length,
                const char* first, const char* second) {
        if (!first)