Skip to contentSkip to conversation

lib/vsprintf: Print time64_t in human readable format

PATCH Superseded Andy Shevchenko 6 participants 19 messages Jan 4, 2019 – Oct 1, 2019 2 messages not in the archive

Subsystems (8)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 1/4 lib/vsprintf: Print time64_t in human readable format

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.

Cc Hans Verkuil <[email protected]>
Cc Mathias Nyman <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
 Documentation/core-api/printk-formats.rst | 22 +++++++------
 lib/test_printf.c                         | 11 +++++--
 lib/vsprintf.c                            | 39 ++++++++++++++++++++++-
 3 files changed, 59 insertions(+), 13 deletions(-)
Documentation/core-api/printk-formats.rst +12 -10
index a7fae4538946..e33c130db76a 100644
@@ -412,21 +412,23 @@ Examples::
 
 Passed by reference.
 
-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.
 
lib/test_printf.c +9 -2
index 659b6cc0d483..a0295fbd874f 100644
@@ -450,7 +450,7 @@ struct_va_format(void)
 }
 
 static void __init
-struct_rtc_time(void)
+time_and_date(void)
 {
 	/* 1543210543 */
 	const struct rtc_time tm = {
@@ -461,6 +461,8 @@ struct_rtc_time(void)
 		.tm_mon = 10,
 		.tm_year = 118,
 	};
+	/* 2019-01-04T15:32:23 */
+	time64_t t = 1546615943;
 
 	test_hashed("%pt", &tm);
 
@@ -470,6 +472,11 @@ struct_rtc_time(void)
 	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
@@ -583,7 +590,7 @@ test_pointer(void)
 	uuid();
 	dentry();
 	struct_va_format();
-	struct_rtc_time();
+	time_and_date();
 	struct_clk();
 	bitmap();
 	netdev_features();
lib/vsprintf.c +38 -1
index 3add92329bae..0457af8b10a9 100644
@@ -31,6 +31,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>
@@ -1633,6 +1634,39 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
 	return buf;
 }
 
+static noinline_for_stack
+void time64_to_rtc_time(time64_t time, struct rtc_time *rtc_time)
+{
+#ifdef CONFIG_RTC_LIB
+	rtc_time64_to_tm(time, rtc_time);
+#else
+	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;
+#endif
+}
+
+static noinline_for_stack
+char *time64_str(char *buf, char *end, const time64_t *t, const char *fmt)
+{
+	struct rtc_time tm;
+
+	time64_to_rtc_time(*t, &tm);
+
+	return rtc_str(buf, end, &tm, fmt);
+}
+
 static noinline_for_stack
 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
 		    const char *fmt)
@@ -1640,6 +1674,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, fmt);
+	case 'T':
+		return time64_str(buf, end, (const time64_t *)ptr, fmt);
 	default:
 		return ptr_to_id(buf, end, ptr, spec);
 	}
@@ -1924,8 +1960,9 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
  * - '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
Signature · 1 line
2.19.2
author reviewer
#1 lore raw

PATCH 2/4 ARM: bcm2835: Switch to use %ptT

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

---
 drivers/firmware/raspberrypi.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/firmware/raspberrypi.c +3 -9
index a13558154ac3..ac5aeab1eb04 100644
@@ -181,16 +181,10 @@ rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
 					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
 					&packet, sizeof(packet));
 
-	if (ret == 0) {
-		struct tm tm;
-
-		time64_to_tm(packet, 0, &tm);
+	if (ret)
+		return;
 
-		dev_info(fw->cl.dev,
-			 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
-			 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-			 tm.tm_hour, tm.tm_min);
-	}
+	dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &packet);
 }
 
 static void
Signature · 1 line
2.19.2
author reviewer
#2 lore raw

PATCH 3/4 usb: pulse8-cec: Switch to use %ptT

In reply to Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

---
 drivers/media/usb/pulse8-cec/pulse8-cec.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/media/usb/pulse8-cec/pulse8-cec.c +1 -5
index b085b14f3f87..05f997e9ce28 100644
@@ -328,7 +328,6 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	u8 *data = pulse8->data + 1;
 	u8 cmd[2];
 	int err;
-	struct tm tm;
 	time64_t date;
 
 	pulse8->vers = 0;
@@ -349,10 +348,7 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	if (err)
 		return err;
 	date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
-	time64_to_tm(date, 0, &tm);
-	dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
-		 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-		 tm.tm_hour, tm.tm_min, tm.tm_sec);
+	dev_info(pulse8->dev, "Firmware build date %ptT\n", &date);
 
 	dev_dbg(pulse8->dev, "Persistent config:\n");
 	cmd[0] = MSGCODE_GET_AUTO_ENABLED;
Signature · 1 line
2.19.2
author reviewer
#3 lore raw

Re: [PATCH v1 3/4] [media] usb: pulse8-cec: Switch to use %ptT

Quoted 2 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Hans, any objections on this?

Quoted 33 lines
---
 drivers/media/usb/pulse8-cec/pulse8-cec.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/media/usb/pulse8-cec/pulse8-cec.c +1 -5
index b085b14f3f87..05f997e9ce28 100644
@@ -328,7 +328,6 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	u8 *data = pulse8->data + 1;
 	u8 cmd[2];
 	int err;
-	struct tm tm;
 	time64_t date;
 
 	pulse8->vers = 0;
@@ -349,10 +348,7 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	if (err)
 		return err;
 	date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
-	time64_to_tm(date, 0, &tm);
-	dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
-		 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-		 tm.tm_hour, tm.tm_min, tm.tm_sec);
+	dev_info(pulse8->dev, "Firmware build date %ptT\n", &date);
 
 	dev_dbg(pulse8->dev, "Persistent config:\n");
 	cmd[0] = MSGCODE_GET_AUTO_ENABLED;
Signature · 1 line
2.19.2
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by
#4 lore raw

Re: [PATCH v1 3/4] [media] usb: pulse8-cec: Switch to use %ptT

Quoted 8 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Hans, any objections on this?

Quoted 2 lines

Thanks!

Hans

Quoted 32 lines
Quoted 31 lines
---
 drivers/media/usb/pulse8-cec/pulse8-cec.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/media/usb/pulse8-cec/pulse8-cec.c +1 -5
index b085b14f3f87..05f997e9ce28 100644
@@ -328,7 +328,6 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	u8 *data = pulse8->data + 1;
 	u8 cmd[2];
 	int err;
-	struct tm tm;
 	time64_t date;
 
 	pulse8->vers = 0;
@@ -349,10 +348,7 @@ static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 	if (err)
 		return err;
 	date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
-	time64_to_tm(date, 0, &tm);
-	dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
-		 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-		 tm.tm_hour, tm.tm_min, tm.tm_sec);
+	dev_info(pulse8->dev, "Firmware build date %ptT\n", &date);
 
 	dev_dbg(pulse8->dev, "Persistent config:\n");
 	cmd[0] = MSGCODE_GET_AUTO_ENABLED;
Signature · 1 line
2.19.2
author reviewer
#5 lore raw

PATCH 4/4 usb: host: xhci-tegra: Switch to use %ptT

In reply to Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Cc Mathias Nyman <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
 drivers/usb/host/xhci-tegra.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-tegra.c +1 -5
index 938ff06c0349..ed3eea3876e2 100644
@@ -820,7 +820,6 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	const struct firmware *fw;
 	unsigned long timeout;
 	time64_t timestamp;
-	struct tm time;
 	u64 address;
 	u32 value;
 	int err;
@@ -925,11 +924,8 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	}
 
 	timestamp = le32_to_cpu(header->fwimg_created_time);
-	time64_to_tm(timestamp, 0, &time);
 
-	dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-		 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-		 time.tm_hour, time.tm_min, time.tm_sec);
+	dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);
 
 	return 0;
 }
Signature · 1 line
2.19.2
author reviewer
#6 lore raw

Re: [PATCH v1 4/4] usb: host: xhci-tegra: Switch to use %ptT

Quoted 2 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Any comments on this?

Quoted 37 lines
Cc Mathias Nyman <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
 drivers/usb/host/xhci-tegra.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-tegra.c +1 -5
index 938ff06c0349..ed3eea3876e2 100644
@@ -820,7 +820,6 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	const struct firmware *fw;
 	unsigned long timeout;
 	time64_t timestamp;
-	struct tm time;
 	u64 address;
 	u32 value;
 	int err;
@@ -925,11 +924,8 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	}
 
 	timestamp = le32_to_cpu(header->fwimg_created_time);
-	time64_to_tm(timestamp, 0, &time);
 
-	dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-		 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-		 time.tm_hour, time.tm_min, time.tm_sec);
+	dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);
 
 	return 0;
 }
Signature · 1 line
2.19.2
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw

Re: [PATCH v1 4/4] usb: host: xhci-tegra: Switch to use %ptT

Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Any comments on this?

Untested, but looks ok to me.

xhci-tegra.c is written by Thierry Reding, so its more up to him

Quoted 39 lines
Quoted 38 lines
Cc Mathias Nyman <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
  drivers/usb/host/xhci-tegra.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-tegra.c +1 -5
index 938ff06c0349..ed3eea3876e2 100644
@@ -820,7 +820,6 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  	const struct firmware *fw;
  	unsigned long timeout;
  	time64_t timestamp;
-	struct tm time;
  	u64 address;
  	u32 value;
  	int err;
@@ -925,11 +924,8 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  	}
 
  	timestamp = le32_to_cpu(header->fwimg_created_time);
-	time64_to_tm(timestamp, 0, &time);
 
-	dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-		 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-		 time.tm_hour, time.tm_min, time.tm_sec);
+	dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);
 
  	return 0;
  }
Signature · 1 line
2.19.2
author reviewer
#8 lore raw

Re: [PATCH v1 4/4] usb: host: xhci-tegra: Switch to use %ptT

Quoted 8 lines · Mathias Nyman
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Any comments on this?

Untested, but looks ok to me.

Thanks!

Quoted 1 line

xhci-tegra.c is written by Thierry Reding, so its more up to him

Thierry, do you have any objections here?

Quoted 38 lines
Quoted 37 lines
Quoted 36 lines
Cc Mathias Nyman <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
  drivers/usb/host/xhci-tegra.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-tegra.c +1 -5
index 938ff06c0349..ed3eea3876e2 100644
@@ -820,7 +820,6 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  	const struct firmware *fw;
  	unsigned long timeout;
  	time64_t timestamp;
-	struct tm time;
  	u64 address;
  	u32 value;
  	int err;
@@ -925,11 +924,8 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  	}
  	timestamp = le32_to_cpu(header->fwimg_created_time);
-	time64_to_tm(timestamp, 0, &time);
-	dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-		 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-		 time.tm_hour, time.tm_min, time.tm_sec);
+	dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);
  	return 0;
  }
Signature · 1 line
2.19.2
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

Re: [PATCH v1 4/4] usb: host: xhci-tegra: Switch to use %ptT

In reply to Andy Shevchenko
Quoted 24 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

Cc Mathias Nyman <[email protected]>
Cc Thierry Reding <[email protected]>
Cc Jonathan Hunter <[email protected]>
---
 drivers/usb/host/xhci-tegra.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/usb/host/xhci-tegra.c +0 -1
index 938ff06c0349..ed3eea3876e2 100644
@@ -820,7 +820,6 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	const struct firmware *fw;
 	unsigned long timeout;
 	time64_t timestamp;
-	struct tm time;
 	u64 address;
 	u32 value;
 	int err;

drivers/usb/host/xhci-tegra.c static int tegra_xusb_load_firmware(struct tegra_xusb *tegra) · line 928

Quoted 9 lines
drivers/usb/host/xhci-tegra.c +1 -4
@@ -925,11 +924,8 @@ static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
 	}
 
 	timestamp = le32_to_cpu(header->fwimg_created_time);
-	time64_to_tm(timestamp, 0, &time);
 
-	dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-		 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-		 time.tm_hour, time.tm_min, time.tm_sec);
+	dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);

If I understand correctly, this will now print:

Firmware timestamp: YYYY-mm-ddTHH:MM:SS UTC

whereas it earlier printed:

Firmware timestamp: YYYY-mm-dd HH:MM:SS UTC

So the 'T' character is different now. Could we make this something along the lines of:

	dev_info(dev, "Firmware timestamp: %ptTd %ptTt UTC\n", &timestamp,
		 &timestamp);

To keep the output identical? It's possible that there are some scripts that parse the log to find out which firmware was loaded.

Thierry

#10 lore raw

Re: [PATCH v1 4/4] usb: host: xhci-tegra: Switch to use %ptT

Quoted 19 lines · Thierry Reding

drivers/usb/host/xhci-tegra.c static int tegra_xusb_load_firmware(struct tegra_xusb *tegra) · line 928

Quoted 7 lines · Andy Shevchenko

Use %ptT instead of open coded variant to print content of time64_t type in human readable format.

drivers/usb/host/xhci-tegra.c +1 -3
-     dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
-              time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
-              time.tm_hour, time.tm_min, time.tm_sec);
+     dev_info(dev, "Firmware timestamp: %ptT UTC\n", &timestamp);

If I understand correctly, this will now print:

Firmware timestamp: YYYY-mm-ddTHH:MM:SS UTC

whereas it earlier printed:

Firmware timestamp: YYYY-mm-dd HH:MM:SS UTC

So the 'T' character is different now.

Quoted 7 lines

Could we make this something
along the lines of:

dev_info(dev, "Firmware timestamp: %ptTd %ptTt UTC\n", &timestamp,
&timestamp);

To keep the output identical?

Yes, we can...

Quoted 2 lines

It's possible that there are some scripts
that parse the log to find out which firmware was loaded.

...but if you have scripts parsing kernel log, something is odd. As far as I understand kernel log isn't ABI, no-one should rely on its output.

Signature · 2 lines
With Best Regards,
Andy Shevchenko

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#13 lore raw
Quoted 17 lines · Alexandre Belloni
Quoted 16 lines · Petr Mladek

lib/vsprintf.c char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt) · line 1641

Quoted 11 lines · Andy Shevchenko

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.

lib/vsprintf.c +4 -0
+void time64_to_rtc_time(time64_t time, struct rtc_time *rtc_time)
+{
+#ifdef CONFIG_RTC_LIB
+	rtc_time64_to_tm(time, rtc_time);

I wonder if the conversion between struct tm and rtc_time might be useful in general.

Quoted 8 lines
Quoted 2 lines

It might make sense to de-duplicate time64_to_tm() and rtc_time64_to_tm() implementations.

Looking at 57f1f0874f42, this seemed to be the plan at the time time_to_tm was introduced but this was never done. Seeing that tm and rtc_time are quite similar, we could probably always use time64_to_tm as it is more accurate than rtc_time64_to_tm as the latter assumes a specific year range.

So, do I understand correctly that dropping #ifdef along with rtc_time64_to_tm() call is sufficient for now?

Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
lib/vsprintf.c +27 -0
+#else
+	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;
+#endif
+}
+
+static noinline_for_stack
+char *time64_str(char *buf, char *end, const time64_t *t, const char *fmt)
+{
+	struct rtc_time tm;
+
+	time64_to_rtc_time(*t, &tm);
+
+	return rtc_str(buf, end, &tm, fmt);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
Quoted 21 lines · Andy Shevchenko
Quoted 19 lines · Alexandre Belloni
Quoted 18 lines · Petr Mladek

lib/vsprintf.c char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt) · line 1641

Quoted 11 lines · Andy Shevchenko

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.

lib/vsprintf.c +4 -0
+void time64_to_rtc_time(time64_t time, struct rtc_time *rtc_time)
+{
+#ifdef CONFIG_RTC_LIB
+	rtc_time64_to_tm(time, rtc_time);

I wonder if the conversion between struct tm and rtc_time might be useful in general.

It might make sense to de-duplicate time64_to_tm() and rtc_time64_to_tm() implementations.

Quoted 9 lines
Quoted 5 lines

Looking at 57f1f0874f42, this seemed to be the plan at the time time_to_tm was introduced but this was never done. Seeing that tm and rtc_time are quite similar, we could probably always use time64_to_tm as it is more accurate than rtc_time64_to_tm as the latter assumes a specific year range.

So, do I understand correctly that dropping #ifdef along with rtc_time64_to_tm() call is sufficient for now?

I'd be fine with that.

Quoted 6 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm. I'm not sure was was the rationale to have a long, especially since userspace has an int.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author reviewer
#15 lore raw
Quoted 28 lines · Alexandre Belloni
Quoted 27 lines · Andy Shevchenko
Quoted 25 lines · Alexandre Belloni
Quoted 16 lines · Petr Mladek

lib/vsprintf.c char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt) · line 1641

Quoted 11 lines · Andy Shevchenko

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.

lib/vsprintf.c +4 -0
+void time64_to_rtc_time(time64_t time, struct rtc_time *rtc_time)
+{
+#ifdef CONFIG_RTC_LIB
+	rtc_time64_to_tm(time, rtc_time);

I wonder if the conversion between struct tm and rtc_time might be useful in general.

Quoted 2 lines

It might make sense to de-duplicate time64_to_tm() and rtc_time64_to_tm() implementations.

Looking at 57f1f0874f42, this seemed to be the plan at the time time_to_tm was introduced but this was never done. Seeing that tm and rtc_time are quite similar, we could probably always use time64_to_tm as it is more accurate than rtc_time64_to_tm as the latter assumes a specific year range.

Quoted 5 lines
Quoted 3 lines

So, do I understand correctly that dropping #ifdef along with rtc_time64_to_tm() call is sufficient for now?

I'd be fine with that.

Good, thanks! I'll send v2 soon.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

Quoted 2 lines

I'm not sure was was the rationale to have a long, especially since userspace has an int.

Yeah, this is strange, I guess we simple may long -> int in kernel's struct tm.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Alexandre Belloni
Quoted 3 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

I don't get why, as long as the first members of both structs are the same, this should work.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author reviewer
#17 lore raw
Quoted 11 lines · Alexandre Belloni
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Alexandre Belloni
Quoted 5 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

Quoted 6 lines
Quoted 3 lines

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

I don't get why, as long as the first members of both structs are the same, this should work.

On BE 64-bit we will always get tm_year == 0, won't we?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 11 lines · Alexandre Belloni
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Alexandre Belloni
Quoted 5 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

I don't get why, as long as the first members of both structs are the same, this should work.

On BE 64-bit we will always get tm_year == 0, won't we?

Not if you have int tm_year in struct tm. I guess we can change the kernel struct tm because it is not part of the ABI.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author reviewer
#19 lore raw
Quoted 17 lines · Alexandre Belloni
Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Alexandre Belloni
Quoted 13 lines · Andy Shevchenko
Quoted 8 lines · Alexandre Belloni
Quoted 5 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I don't get why, as long as the first members of both structs are the same, this should work.

On BE 64-bit we will always get tm_year == 0, won't we?

Not if you have int tm_year in struct tm. I guess we can change the kernel struct tm because it is not part of the ABI.

We can, but:
- it will require to change all printf("%ld", tm_year) cases at the same time in entire kernel (and also some functions might start producing warnings when some variable will be cut to int)
- it is out of scope of this series

So, I will leave it untouched for now.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw
Quoted 27 lines · Andy Shevchenko
Quoted 17 lines · Alexandre Belloni
Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Alexandre Belloni
Quoted 13 lines · Andy Shevchenko
Quoted 8 lines · Alexandre Belloni
Quoted 5 lines
Quoted 2 lines

Maybe be rtc_str should take a struct tm instead of an rtc_time so time64_to_rtc_time always uses time64_to_tm.

Because this one, while sounding plausible, maybe too invasive on current state of affairs.

Well, if the kernel struct tm had an int tm_year instead of long tm_year, then you could simply cast a struct rtc_time to a struct tm.

I don't think so. It will be error prone from endianess prospective on 64-bit platforms.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I don't get why, as long as the first members of both structs are the same, this should work.

On BE 64-bit we will always get tm_year == 0, won't we?

Not if you have int tm_year in struct tm. I guess we can change the kernel struct tm because it is not part of the ABI.

Quoted 8 lines

We can, but:
- it will require to change all printf("%ld", tm_year) cases at the same time in entire kernel (and also some functions might start producing warnings when some variable will be cut to int)
- it is out of scope of this series

So, I will leave it untouched for now.

That's fine, I was not asking to do it as a prerequisite ;)

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com