]> Gentwo Git Trees - linux/.git/commitdiff
lib/vsprintf: Add specifier for printing struct timespec64
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Thu, 13 Nov 2025 14:32:15 +0000 (15:32 +0100)
committerPetr Mladek <pmladek@suse.com>
Wed, 19 Nov 2025 09:24:13 +0000 (10:24 +0100)
A handful drivers want to print a content of the struct timespec64
in a format of %lld:%09ld. In order to make their lives easier, add
the respecting specifier directly to the printf() implementation.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
Link: https://patch.msgid.link/20251113150217.3030010-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Petr Mladek <pmladek@suse.com>
Documentation/core-api/printk-formats.rst
lib/tests/printf_kunit.c
lib/vsprintf.c

index 7f2f11b482865175971f0f1c11fa45b96e5b0f07..c0b1b6089307a38b6d7ef99940268d1ffce00716 100644 (file)
@@ -547,11 +547,13 @@ Time and date
        %pt[RT]s                YYYY-mm-dd HH:MM:SS
        %pt[RT]d                YYYY-mm-dd
        %pt[RT]t                HH:MM:SS
-       %pt[RT][dt][r][s]
+       %ptSp                   <seconds>.<nanoseconds>
+       %pt[RST][dt][r][s]
 
 For printing date and time as represented by::
 
-       R  struct rtc_time structure
+       R  content of struct rtc_time
+       S  content of struct timespec64
        T  time64_t type
 
 in human readable format.
@@ -563,6 +565,11 @@ The %pt[RT]s (space) will override ISO 8601 separator by using ' ' (space)
 instead of 'T' (Capital T) between date and time. It won't have any effect
 when date or time is omitted.
 
+The %ptSp is equivalent to %lld.%09ld for the content of the struct timespec64.
+When the other specifiers are given, it becomes the respective equivalent of
+%ptT[dt][r][s].%09ld. In other words, the seconds are being printed in
+the human readable format followed by a dot and nanoseconds.
+
 Passed by reference.
 
 struct clk
index bc54cca2d7a6fe659e08e61348635e1ec94896d4..7617e5b8b02cc2ae63f636cba2d96b02fe94f112 100644 (file)
@@ -504,6 +504,7 @@ time_and_date(struct kunit *kunittest)
        };
        /* 2019-01-04T15:32:23 */
        time64_t t = 1546615943;
+       struct timespec64 ts = { .tv_sec = t, .tv_nsec = 11235813 };
 
        test("(%pt?)", "%pt", &tm);
        test("2018-11-26T05:35:43", "%ptR", &tm);
@@ -522,6 +523,9 @@ time_and_date(struct kunit *kunittest)
        test("0119-00-04 15:32:23", "%ptTsr", &t);
        test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
        test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
+
+       test("2019-01-04T15:32:23.011235813", "%ptS", &ts);
+       test("1546615943.011235813", "%ptSp", &ts);
 }
 
 static void
index eb0cb11d0d126f02d8884b3ebbcdcf75beb8a621..cdfb9224e0ad998ccf7a45b7f97e3c376ae98e95 100644 (file)
@@ -1992,6 +1992,28 @@ char *time64_str(char *buf, char *end, const time64_t time,
        return rtc_str(buf, end, &rtc_time, spec, fmt);
 }
 
+static noinline_for_stack
+char *timespec64_str(char *buf, char *end, const struct timespec64 *ts,
+                    struct printf_spec spec, const char *fmt)
+{
+       static const struct printf_spec default_dec09_spec = {
+               .base = 10,
+               .field_width = 9,
+               .precision = -1,
+               .flags = ZEROPAD,
+       };
+
+       if (fmt[2] == 'p')
+               buf = number(buf, end, ts->tv_sec, default_dec_spec);
+       else
+               buf = time64_str(buf, end, ts->tv_sec, spec, fmt);
+       if (buf < end)
+               *buf = '.';
+       buf++;
+
+       return number(buf, end, ts->tv_nsec, default_dec09_spec);
+}
+
 static noinline_for_stack
 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
                    const char *fmt)
@@ -1999,6 +2021,8 @@ char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
        switch (fmt[1]) {
        case 'R':
                return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
+       case 'S':
+               return timespec64_str(buf, end, (const struct timespec64 *)ptr, spec, fmt);
        case 'T':
                return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
        default:
@@ -2462,9 +2486,11 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
  * - 'd[234]' For a dentry name (optionally 2-4 last components)
  * - 'D[234]' Same as 'd' but for a struct file
  * - 'g' For block_device name (gendisk + partition number)
- * - 't[RT][dt][r][s]' For time and date as represented by:
+ * - 't[RST][dt][r][s]' For time and date as represented by:
  *      R    struct rtc_time
+ *      S    struct timespec64
  *      T    time64_t
+ * - 'tSp' For time represented by struct timespec64 printed as <seconds>.<nanoseconds>
  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  *       (legacy clock framework) of the clock
  * - 'G' For flags to be printed as a collection of symbolic strings that would