[PATCH] date: accept 'yyyy-mm-dd HH' and 'yyyy-mm-dd' date formats.

Bartosz Gołaszewski bartekgola at gmail.com
Tue Jul 9 18:25:09 UTC 2013


2013/5/22 Bartosz Golaszewski <bartekgola at gmail.com>:
> This makes busybox date more compatible with coreutils and fixes a TODO
> in libbb/time.c.
>
> Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
> ---
>  libbb/time.c |   16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/libbb/time.c b/libbb/time.c
> index 57e14b6..3d8305d 100644
> --- a/libbb/time.c
> +++ b/libbb/time.c
> @@ -58,7 +58,6 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
>                         return; /* don't fall through to end == ":" check */
>                 } else
>  #endif
> -//TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
>                 {
>                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
>                 }
> @@ -68,7 +67,20 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
>                                 end = '\0';
>                         /* else end != NUL and we error out */
>                 }
> -       } else if (date_str[0] == '@') {
> +       } else
> +       /* yyyy-mm-dd HH */
> +       if ((sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
> +                               &ptm->tm_mon, &ptm->tm_mday,
> +                               &ptm->tm_hour, &end) >= 4) ||
> +       /* yyyy-mm-dd */
> +               (sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
> +                               &ptm->tm_mon, &ptm->tm_mday, &end) >= 3))
> +       {
> +               ptm->tm_year -= 1900; /* Adjust years */
> +               ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
> +       } else
> +       /* unix timestamp */
> +       if (date_str[0] == '@') {
>                 time_t t = bb_strtol(date_str + 1, NULL, 10);
>                 if (!errno) {
>                         struct tm *lt = localtime(&t);
> --
> 1.7.10.4
>

Ping.

Best Regards,
Bartosz Gołaszewski


More information about the busybox mailing list