Skip to content

Commit 20bc8c1

Browse files
andy-shevpmladek
authored andcommitted
lib/vsprintf: Allow to override ISO 8601 date and time separator
ISO 8601 defines 'T' as a separator between date and time. Though, some ABIs use time and date with ' ' (space) separator instead. Add a flavour to the %pt specifier to override default separator. Signed-off-by: Andy Shevchenko <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7f3d08b commit 20bc8c1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Documentation/core-api/printk-formats.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ Time and date
514514
::
515515

516516
%pt[RT] YYYY-mm-ddTHH:MM:SS
517+
%pt[RT]s YYYY-mm-dd HH:MM:SS
517518
%pt[RT]d YYYY-mm-dd
518519
%pt[RT]t HH:MM:SS
519-
%pt[RT][dt][r]
520+
%pt[RT][dt][r][s]
520521

521522
For printing date and time as represented by::
522523

@@ -528,6 +529,10 @@ in human readable format.
528529
By default year will be incremented by 1900 and month by 1.
529530
Use %pt[RT]r (raw) to suppress this behaviour.
530531

532+
The %pt[RT]s (space) will override ISO 8601 separator by using ' ' (space)
533+
instead of 'T' (Capital T) between date and time. It won't have any effect
534+
when date or time is omitted.
535+
531536
Passed by reference.
532537

533538
struct clk

lib/test_printf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,11 @@ time_and_date(void)
528528
test("0119-00-04T15:32:23", "%ptTr", &t);
529529
test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t);
530530
test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t);
531+
532+
test("2019-01-04 15:32:23", "%ptTs", &t);
533+
test("0119-00-04 15:32:23", "%ptTsr", &t);
534+
test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t);
535+
test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t);
531536
}
532537

533538
static void __init

lib/vsprintf.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,8 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
17981798
struct printf_spec spec, const char *fmt)
17991799
{
18001800
bool have_t = true, have_d = true;
1801-
bool raw = false;
1801+
bool raw = false, iso8601_separator = true;
1802+
bool found = true;
18021803
int count = 2;
18031804

18041805
if (check_pointer(&buf, end, tm, spec))
@@ -1815,14 +1816,25 @@ char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
18151816
break;
18161817
}
18171818

1818-
raw = fmt[count] == 'r';
1819+
do {
1820+
switch (fmt[count++]) {
1821+
case 'r':
1822+
raw = true;
1823+
break;
1824+
case 's':
1825+
iso8601_separator = false;
1826+
break;
1827+
default:
1828+
found = false;
1829+
break;
1830+
}
1831+
} while (found);
18191832

18201833
if (have_d)
18211834
buf = date_str(buf, end, tm, raw);
18221835
if (have_d && have_t) {
1823-
/* Respect ISO 8601 */
18241836
if (buf < end)
1825-
*buf = 'T';
1837+
*buf = iso8601_separator ? 'T' : ' ';
18261838
buf++;
18271839
}
18281840
if (have_t)
@@ -2261,7 +2273,7 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
22612273
* - 'd[234]' For a dentry name (optionally 2-4 last components)
22622274
* - 'D[234]' Same as 'd' but for a struct file
22632275
* - 'g' For block_device name (gendisk + partition number)
2264-
* - 't[RT][dt][r]' For time and date as represented by:
2276+
* - 't[RT][dt][r][s]' For time and date as represented by:
22652277
* R struct rtc_time
22662278
* T time64_t
22672279
* - 'C' For a clock, it prints the name (Common Clock Framework) or address

0 commit comments

Comments
 (0)