]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add function to format timeval to ISO 8601 format
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 11:39:02 +0000 (11:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 11:39:02 +0000 (11:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h
src/pakfire/util.h

index edd4211dc38a19cb2b86687b62581e59d07e8dc9..4bca3ee84c8d6dbf369092abe4cd1e85c34495d4 100644 (file)
@@ -672,6 +672,18 @@ int __pakfire_format_time(char* buffer, const size_t length, const time_t t) {
 }
 #pragma GCC diagnostic pop
 
+int __pakfire_timeval_to_iso8601(char* buffer, const size_t length, const struct timeval* t) {
+       struct tm tm = {};
+
+       // Convert to broken down time
+       if (!gmtime_r(&t->tv_sec, &tm))
+               return -errno;
+
+       return __pakfire_string_format(buffer, length, "%04d-%02d-%02dT%02d:%02d:%02d.%03ldZ",
+               tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
+               US_TO_MS(t->tv_usec));
+}
+
 char* pakfire_string_format_interval(const time_t t) {
        char* buffer = NULL;
        int r;
index b7fd8d65b74178f32fa4d36c444a15baf9ac108b..33a6ce6b7d76add43bb45619bda700d7711e2a26 100644 (file)
@@ -135,6 +135,10 @@ int __pakfire_strftime_now(char* buffer, const size_t length, const char* format
        __pakfire_format_time(buffer, sizeof(buffer), t)
 int __pakfire_format_time(char* buffer, const size_t length, const time_t t);
 
+#define pakfire_timeval_to_iso8601(buffer, t) \
+       __pakfire_timeval_to_iso8601(buffer, sizeof(buffer), t)
+int __pakfire_timeval_to_iso8601(char* buffer, const size_t length, const struct timeval* t);
+
 size_t pakfire_string_parse_bytes(const char* s);
 
 char* pakfire_string_format_interval(const time_t t);
index 86b9bf76e3d1dd00d9872740752ecc63065928c6..06f6dc2717b40774c9cc9c41cf644a8ec0dcb326 100644 (file)
@@ -110,6 +110,8 @@ int pakfire_copy(struct pakfire_ctx* ctx, FILE* src, FILE* dst);
 #define US_TO_S(s) (s / 1000000)
 #define NS_TO_S(s) (s / 1000000000)
 
+#define US_TO_MS(us) (us / 1000)
+
 static inline double pakfire_timespec_delta(struct timespec* t1, struct timespec* t2) {
        // Compute delta in seconds
        return (