git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 2/4] date.c: validate and set time in a helper function

From
Danh Doan <congdanhqx@gmail.com>
Date
Apr 24, 2020, 11:43 UTC
Message-ID
<20200424114302.GG1949@danh.dev>
In-Reply-To
<xmqqzhb2ez1q.fsf@gitster.c.googlers.com>
On 2020-04-23 13:18:25-0700, Junio C Hamano <gitster@pobox.com> wrote:
Show 42 quoted lines
> Đoàn Trần Công Danh  <congdanhqx@gmail.com> writes:
> 
> > In a later patch, we will reuse this logic, move it to a helper, now.
> >
> > While we're at it, explicit states that we intentionally ignore
> 
> "explicitly state", perhaps.
> 
> > old-and-defective 2nd leap second.
> >
> > Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
> > ---
> >  date.c | 22 +++++++++++++++++-----
> >  1 file changed, 17 insertions(+), 5 deletions(-)
> >
> > diff --git a/date.c b/date.c
> > index b67c5abe24..f5d5a91208 100644
> > --- a/date.c
> > +++ b/date.c
> > @@ -539,6 +539,22 @@ static int set_date(int year, int month, int day, struct tm *now_tm, time_t now,
> >  	return -1;
> >  }
> >  
> > +static int set_time(long hour, long minute, long second, struct tm *tm)
> > +{
> > +	/* C90 and old POSIX accepts 2 leap seconds, it's a defect,
> > +	 * ignore second number 61
> > +	 */
> 
> 	/*
> 	 * Style: our multi-line comments ought to be
> 	 * formatted like this.  Slash-asterisk that opens,
> 	 * and asterisk-slash that closes, are both on their
> 	 * own lines.
> 	 */
> 
> But I am not sure we want to even have a new comment here.  After
> all we are extracting/reinventing exactly the same logic as the
> original.  Why we allow "60" might be worth commenting, but if a
> minute that has 62 seconds is a mere historical curiosity, then is
> it worth explaining why "61", which we never even wrote in the code,
> is missing from here?
I think single line like:
	/* We accept 61st second for the single? leap second */

Or something along the time, is good enough. Not sure if we want the word "single" there, though.

I think majority of people don't even know about leap second. Probability that know about 62nd second is rarer, I think.

Show 36 quoted lines
> > +	if (0 <= hour && hour <= 24 &&
> > +	    0 <= minute && minute < 60 &&
> > +	    0 <= second && second <= 60) {
> > +		tm->tm_hour = hour;
> > +		tm->tm_min = minute;
> > +		tm->tm_sec = second;
> > +		return 0;
> > +	}
> > +	return -1;
> > +}
> 
> I am a bit surprised to see that you chose to unify with the "check
> and set" interface of is_date (now set_date).  I was expecting to
> see that we'd have "check-only" helper functions.
> 
> This is not a complaint, at least not yet until we see the result of
> using it in new code; it may very well be possible that the "check
> and set" interface would make the new caller(s) clearer.
> 
> >  static int match_multi_number(timestamp_t num, char c, const char *date,
> >  			      char *end, struct tm *tm, time_t now)
> >  {
> > @@ -556,12 +572,8 @@ static int match_multi_number(timestamp_t num, char c, const char *date,
> >  	case ':':
> >  		if (num3 < 0)
> >  			num3 = 0;
> > -		if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
> > -			tm->tm_hour = num;
> > -			tm->tm_min = num2;
> > -			tm->tm_sec = num3;
> > +		if (set_time(num, num2, num3, tm) == 0)
> >  			break;
> > -		}
> >  		return 0;
> 
> This caller does become easier to follow, I would say.  Nicely done.
Yes, when I looked around date.c

I saw that the only usecase for validate time is for setting it. And the incoming patch also has that usage.

I chose to unify those code path to not buy me too much trouble.

I'll take that "Nicely done" means this unification is OK for this series.

-- 
Danh
Previous: Junio C HamanoNext: Junio C Hamano
Message 40 of 49 in “Mishandling of fractional seconds in ISO 8601 format”
  1. brian m. carlsonApr 14, 2020
  2. 0/2 More ISO-8601 supportĐoàn Trần Công Danh, Apr 14, 2020
  3. 1/2 date.c: allow fractional second part of ISO-8601Đoàn Trần Công Danh, Apr 14, 2020
  4. Jeff KingApr 14, 2020
  5. Danh DoanApr 15, 2020
  6. Jeff KingApr 14, 2020
  7. brian m. carlsonApr 14, 2020
  8. Danh DoanApr 15, 2020
  9. 2/2 date.c: allow compact version of ISO-8601 datetimeĐoàn Trần Công Danh, Apr 14, 2020
  10. Jeff KingApr 14, 2020
  11. Danh DoanApr 15, 2020
  12. Junio C HamanoApr 15, 2020
  13. Jeff KingApr 15, 2020
  14. Junio C HamanoApr 15, 2020
  15. Danh DoanApr 16, 2020
  16. brian m. carlsonApr 14, 2020
  17. 0/2 More ISO-8601 supportĐoàn Trần Công Danh, Apr 15, 2020
  18. 1/2 date.c: skip fractional second part of ISO-8601Đoàn Trần Công Danh, Apr 15, 2020
  19. Torsten BögershausenApr 15, 2020
  20. Danh DoanApr 16, 2020
  21. 2/2 date.c: allow compact version of ISO-8601 datetimeĐoàn Trần Công Danh, Apr 15, 2020
  22. 0/2 More ISO-8601 supportĐoàn Trần Công Danh, Apr 22, 2020
  23. 1/2 date.c: skip fractional second part of ISO-8601Đoàn Trần Công Danh, Apr 22, 2020
  24. Junio C HamanoApr 22, 2020
  25. Danh DoanApr 23, 2020
  26. Junio C HamanoApr 23, 2020
  27. Philip OakleyApr 23, 2020
  28. Danh DoanApr 24, 2020
  29. Junio C HamanoApr 24, 2020
  30. Philip OakleyApr 24, 2020
  31. Philip OakleyApr 24, 2020
  32. 2/2 date.c: allow compact version of ISO-8601 datetimeĐoàn Trần Công Danh, Apr 22, 2020
  33. Junio C HamanoApr 22, 2020
  34. Danh DoanApr 23, 2020
  35. 0/4 More ISO-8601 supportĐoàn Trần Công Danh, Apr 23, 2020
  36. 1/4 date.c: s/is_date/set_date/Đoàn Trần Công Danh, Apr 23, 2020
  37. Junio C HamanoApr 23, 2020
  38. 2/4 date.c: validate and set time in a helper functionĐoàn Trần Công Danh, Apr 23, 2020
  39. Junio C HamanoApr 23, 2020
  40. Danh DoanApr 24, 2020
  41. Junio C HamanoApr 24, 2020
  42. 3/4 date.c: skip fractional second part of ISO-8601Đoàn Trần Công Danh, Apr 23, 2020
  43. Junio C HamanoApr 23, 2020
  44. 4/4 date.c: allow compact version of ISO-8601 datetimeĐoàn Trần Công Danh, Apr 23, 2020
  45. 0/4 More ISO-8601 supportĐoàn Trần Công Danh, Apr 24, 2020
  46. 1/4 date.c: s/is_date/set_date/Đoàn Trần Công Danh, Apr 24, 2020
  47. 2/4 date.c: validate and set time in a helper functionĐoàn Trần Công Danh, Apr 24, 2020
  48. 3/4 date.c: skip fractional second part of ISO-8601Đoàn Trần Công Danh, Apr 24, 2020
  49. 4/4 date.c: allow compact version of ISO-8601 datetimeĐoàn Trần Công Danh, Apr 24, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.