lib/vsprintf: Print time64_t in human readable format
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Wed, 15 Apr 2020 17:00:44 +0000 (20:00 +0300)
committerPetr Mladek <pmladek@suse.com>
Wed, 20 May 2020 12:54:18 +0000 (14:54 +0200)
There are users which print time and date represented by content of
time64_t type in human readable format.

Instead of open coding that each time introduce %ptT[dt][r] specifier.

Few test cases for %ptT specifier has been added as well.

Link: https://lore.kernel.org/r/20200415170046.33374-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Rewieved-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Documentation/core-api/printk-formats.rst
lib/test_printf.c
lib/vsprintf.c

index 8ebe46b1af39d88171acc6055759db0b8799ff20..a407cdd090830be1e96918e76ee506920dc2c702 100644 (file)
@@ -468,21 +468,23 @@ Examples (OF)::
        %pfwf   /ocp@68000000/i2c@48072000/camera@10/port/endpoint - Full name
        %pfwP   endpoint                                - Node name
 
-Time and date (struct rtc_time)
--------------------------------
+Time and date
+-------------
 
 ::
 
-       %ptR            YYYY-mm-ddTHH:MM:SS
-       %ptRd           YYYY-mm-dd
-       %ptRt           HH:MM:SS
-       %ptR[dt][r]
+       %pt[RT]                 YYYY-mm-ddTHH:MM:SS
+       %pt[RT]d                YYYY-mm-dd
+       %pt[RT]t                HH:MM:SS
+       %pt[RT][dt][r]
 
-For printing date and time as represented by struct rtc_time structure in
-human readable format.
+For printing date and time as represented by
+       R  struct rtc_time structure
+       T  time64_t type
+in human readable format.
 
-By default year will be incremented by 1900 and month by 1. Use %ptRr (raw)
-to suppress this behaviour.
+By default year will be incremented by 1900 and month by 1.
+Use %pt[RT]r (raw) to suppress this behaviour.
 
 Passed by reference.
 
index 2d9f520d2f275ca95562a01371a0bd33c9278505..6dc0a6c33b8c062fc06e8bd580f5015c14edd5c1 100644 (file)
@@ -478,7 +478,7 @@ struct_va_format(void)
 }
 
 static void __init
-struct_rtc_time(void)
+time_and_date(void)
 {
        /* 1543210543 */
        const struct rtc_time tm = {
@@ -489,14 +489,21 @@ struct_rtc_time(void)
                .tm_mon = 10,
                .tm_year = 118,
        };
+       /* 2019-01-04T15:32:23 */
+       time64_t t = 1546615943;
 
-       test("(%ptR?)", "%pt", &tm);
+       test("(%pt?)", "%pt", &tm);
        test("2018-11-26T05:35:43", "%ptR", &tm);
        test("0118-10-26T05:35:43", "%ptRr", &tm);
        test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
        test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
        test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
        test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
+
+       test("2019-01-04T15:32:23", "%ptT", &t);
+       test("0119-00-04T15:32:23", "%ptTr", &t);
+       test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
+       test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
 }
 
 static void __init
@@ -661,7 +668,7 @@ test_pointer(void)
        uuid();
        dentry();
        struct_va_format();
-       struct_rtc_time();
+       time_and_date();
        struct_clk();
        bitmap();
        netdev_features();
index 7c488a1ce318cc52c375b0db7d9db2e353dfa836..adbcca9c9cbad6b0f270a23e26c5068da8b9d970 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/dcache.h>
 #include <linux/cred.h>
 #include <linux/rtc.h>
+#include <linux/time.h>
 #include <linux/uuid.h>
 #include <linux/of.h>
 #include <net/addrconf.h>
@@ -1819,6 +1820,29 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
        return buf;
 }
 
+static noinline_for_stack
+char *time64_str(char *buf, char *end, const time64_t time,
+                struct printf_spec spec, const char *fmt)
+{
+       struct rtc_time rtc_time;
+       struct tm tm;
+
+       time64_to_tm(time, 0, &tm);
+
+       rtc_time.tm_sec = tm.tm_sec;
+       rtc_time.tm_min = tm.tm_min;
+       rtc_time.tm_hour = tm.tm_hour;
+       rtc_time.tm_mday = tm.tm_mday;
+       rtc_time.tm_mon = tm.tm_mon;
+       rtc_time.tm_year = tm.tm_year;
+       rtc_time.tm_wday = tm.tm_wday;
+       rtc_time.tm_yday = tm.tm_yday;
+
+       rtc_time.tm_isdst = 0;
+
+       return rtc_str(buf, end, &rtc_time, spec, fmt);
+}
+
 static noinline_for_stack
 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
                    const char *fmt)
@@ -1826,8 +1850,10 @@ 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 'T':
+               return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
        default:
-               return error_string(buf, end, "(%ptR?)", spec);
+               return error_string(buf, end, "(%pt?)", spec);
        }
 }
 
@@ -2143,8 +2169,9 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
  * - '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[R][dt][r]' For time and date as represented:
+ * - 't[RT][dt][r]' For time and date as represented by:
  *      R    struct rtc_time
+ *      T    time64_t
  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  *       (legacy clock framework) of the clock
  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address