Re: [PATCH v2 01/21] lib/vsprintf: Print time and date in human readable format via %pt

From: Joe Perches
Date: Tue Feb 20 2018 - 18:55:22 EST


On Tue, 2018-02-20 at 23:43 +0200, Andy Shevchenko wrote:
> There are users which print time and date represented by content of
> struct rtc_time in human readable format.
>
> Instead of open coding that each time introduce %ptR[dt][rv] specifier.
>
> Note, users have to select PRINTK_PEXT_TIMEDATE option in a Kconfig.

Not sure this is a great option.
Not just the name, the need to select it.

> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
[]
> +static noinline_for_stack
> +char *date_str(char *buf, char *end, const struct rtc_time *tm, bool v, bool r)
> +{
> + int year = tm->tm_year + (r ? 0 : 1900);
> + int mon = tm->tm_mon + (r ? 0 : 1);

What happens with negative values?
Perhaps these temporaries should be unsigned int.

> +
> + if (unlikely(v && (unsigned int)tm->tm_year > 200))
> + buf = string(buf, end, "****", default_str_spec);
> + else
> + buf = number(buf, end, year, default_dec04_spec);
> +
> + if (buf < end)
> + *buf = '-';
> + buf++;
> +
> + if (unlikely(v && (unsigned int)tm->tm_mon > 11))
> + buf = string(buf, end, "**", default_str_spec);
> + else
> + buf = number(buf, end, mon, default_dec02_spec);
> +
> + if (buf < end)
> + *buf = '-';
> + buf++;
> +
> + if (unlikely(v && (unsigned int)tm->tm_mday > 31))
> + buf = string(buf, end, "**", default_str_spec);
> + else
> + buf = number(buf, end, tm->tm_mday, default_dec02_spec);
> +
> + return buf;
> +}
> +
> +static noinline_for_stack
> +char *time_str(char *buf, char *end, const struct rtc_time *tm, bool v, bool r)
> +{

Maybe use unsigned int temporaries here too for hour, min, sec

> + if (unlikely(v && (unsigned int)tm->tm_hour > 24))
> + buf = string(buf, end, "**", default_str_spec);
> + else
> + buf = number(buf, end, tm->tm_hour, default_dec02_spec);
> +
> + if (buf < end)
> + *buf = ':';
> + buf++;
> +
> + if (unlikely(v && (unsigned int)tm->tm_min > 59))

leap seconds are allowed in the struct

> + buf = string(buf, end, "**", default_str_spec);
> + else
> + buf = number(buf, end, tm->tm_min, default_dec02_spec);
> +
> + if (buf < end)
> + *buf = ':';
> + buf++;
> +
> + if (unlikely(v && (unsigned int)tm->tm_sec > 59))
> + buf = string(buf, end, "**", default_str_spec);
> + else
> + buf = number(buf, end, tm->tm_sec, default_dec02_spec);
> +
> + return buf;
> +}
>