Re: [PATCH 2/2] date.c: allow compact version of ISO-8601 datetime
- From
Danh Doan <congdanhqx@gmail.com>
- Date
- Apr 15, 2020, 02:12 UTC
- Message-ID
- <20200415021202.GA19025@danh.dev>
- In-Reply-To
- <20200414202401.GC1879688@coredump.intra.peff.net>
On 2020-04-14 16:24:01-0400, Jeff King <peff@peff.net> wrote:
Show 25 quoted lines
> On Tue, Apr 14, 2020 at 04:31:55PM +0700, Đoàn Trần Công Danh wrote:
>
> > @@ -666,6 +666,24 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
> > n++;
> > } while (isdigit(date[n]));
> >
> > + /* 8 digits, compact style of ISO-8601's date: YYYYmmDD */
> > + if (n == 8) {
> > + tm->tm_year = num / 10000 - 1900;
> > + tm->tm_mon = (num % 10000) / 100 - 1;
> > + tm->tm_mday = num % 100;
> > + return n;
> > + }
>
> I worry a little this may conflict with other approxidate heuristics.
> The only one I can think of is an actual unix timestamp, though, and we
> already require that to have at least 9 digits (plus anybody wanting to
> use one robustly really should be using @12345678).
>
> And it looks like we'd exit early from the function for anything longer
> than 4 digits anyway, ignoring the value.
>
> We could probably tighten the heuristics a bit by insisting that the
> month and day be sensible. Or even year (see the 1900 to 2100 magic for
> the 4-digit year guess).Yeah, It's make sense to tighten the heuristics. While 1900 is lower bound for year makes sense to me, but I don't think we should limit upper bound for tm_year.
Show 13 quoted lines
> > + /* 6 digits, compact style of ISO-8601's time: HHMMSS */
> > + if (n == 6) {
> > + tm->tm_hour = num / 10000;
> > + tm->tm_min = (num % 10000) / 100;
> > + tm->tm_sec = num % 100;
> > + if (*end == '.' && isdigit(end[1]))
> > + strtoul(end + 1, &end, 10);
> > + return end - date;
> > + }
>
> And likewise here that the hour, minute, and second be reasonable.
>
> -Peff-- Danh