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

Re: [PATCH 2/2] approxidate: overwrite tm_mday for `now` and `yesterday`

From
Jeff King <peff@peff.net>
Date
Apr 4, 2025, 08:40 UTC
Message-ID
<20250404084048.GB762635@coredump.intra.peff.net>
In-Reply-To
<20250318180201.3653-3-taahol@utu.fi>
On Tue, Mar 18, 2025 at 08:02:01PM +0200, Tuomas Ahola wrote:
> Date specifications now (or today) and yesterday should refer to
> actual current or previous day.  Especially "noon today" or "noon
> yesterday" should override the usual logic of using the first previous
> noon depending on the current time.

OK. So if I understand the issue correctly, it is that saying "noon" sets the day field of the "struct tm", and then "today" or "yesterday" won't override that, because update_tm() tries not to touch those fields if they're already set.

This presumably works for "10am yesterday" because "10am" just sets the time, and never the date (though I didn't check).

Show 11 quoted lines
> diff --git a/date.c b/date.c
> index 482a2f8c99..2a8a942d64 100644
> --- a/date.c
> +++ b/date.c
> @@ -1121,12 +1121,14 @@ static void pending_number(struct tm *tm, int *num)
>  static void date_now(struct tm *tm, struct tm *now, int *num)
>  {
>  	*num = 0;
> +	tm->tm_mday = -1;
>  	update_tm(tm, now, 0);
>  }

So OK, this approach makes sense: throw out the old date we computed so that update_tm() can use today's.

But is resetting tm_mday enough? If we previously set tm_mon, then update_tm() won't override that, but it would need to follow along with tm_mday, wouldn't it?

So if it is the morning of April 2nd, that will work fine, since yesterday and today have the same month. And it seems to (this is with your patch):

  $ GIT_TEST_DATE_NOW=$(date --date='2025-04-02 11:00' +%s) \
    t/helper/test-tool date approxidate 'noon today'
  noon today -> 2025-04-02 16:00:00 +0000
but if we try the same thing on the 1st:
  $ GIT_TEST_DATE_NOW=$(date --date='2025-04-01 11:00' +%s) \
    t/helper/test-tool date approxidate 'noon today'
  noon today -> 2025-03-01 16:00:00 +0000

Oops, we went back a month. And presumably the same thing would happen with the year on Jan 1st.

Interestingly, since we are passing "0" to update_tm() as its adjustment, I think this might be easier to read as just:

  tm->tm_mday = now->tm_mday;
  tm->tm_mon = now->tm_mon;
  tm->tm_year = now->tm_year;

But it's possible that the round-trip through mktime() and localtime_r() in update_tm() has some value (I wonder what would happen at 2:30am on a day when DST springs forward from 2am to 3am).

Show 6 quoted lines
>  static void date_yesterday(struct tm *tm, struct tm *now, int *num)
>  {
>  	*num = 0;
> +	tm->tm_mday = -1;
>  	update_tm(tm, now, 24*60*60);
>  }

OK, same here, except obviously we _do_ need to call update_tm() for its adjustment.

Show 7 quoted lines
> @@ -1204,6 +1206,7 @@ static const struct special {
>  	{ "AM", date_am },
>  	{ "never", date_never },
>  	{ "now", date_now },
> +	{ "today", date_now },
>  	{ NULL }
>  };

So before we did not understand "today" at all, but just ignored it. Which worked because we try to make everything relative to "now" by default anyway. Adding it as you do here makes sense, since now we are using its ability to "override" the date set by things like "noon".

It is a little weird that "now" and "today" share the same implementation. So notably, "noon now" does not override the time (ignoring "noon" completely). But that is already the case before your patch (and I think is just one of those approxidate "if it hurts, don't do it" quirks).

Show 14 quoted lines
> --- a/t/t0006-date.sh
> +++ b/t/t0006-date.sh
> @@ -178,10 +178,10 @@ check_approxidate '6am yesterday' '2009-08-29 06:00:00'
>  check_approxidate '6pm yesterday' '2009-08-29 18:00:00'
>  check_approxidate '3:00' '2009-08-30 03:00:00'
>  check_approxidate '15:00' '2009-08-30 15:00:00'
> -check_approxidate 'noon today' '2009-08-30 12:00:00'
> -check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
>  (
>  	GIT_TEST_DATE_NOW=$(($GIT_TEST_DATE_NOW-12*60*60)); export GIT_TEST_DATE_NOW
> +	check_approxidate 'noon today' '2009-08-30 12:00:00'
> +	check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
>  	check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
>  )

Same comments on the tests as in patch 1. I think we'd want to add, rather than replace, and put these in the same save/restore block rather than using a subshell.

-Peff
Previous: Tuomas AholaNext: Tuomas Ahola
Message 3 of 36 in “approxidate: tweak special date formats”
  1. 0/2 approxidate: tweak special date formatsTuomas Ahola, Mar 18, 2025
  2. 2/2 approxidate: overwrite tm_mday for `now` and `yesterday`Tuomas Ahola, Mar 18, 2025
  3. Jeff KingApr 4, 2025
  4. 1/2 approxidate: make "specials" respect fixed day-of-monthTuomas Ahola, Mar 18, 2025
  5. Jeff KingApr 4, 2025
  6. 0/3 approxidate: tweak special date formatsTuomas Ahola, May 12, 2026
  7. 1/3 t0006: add support for approxidate test date adjustmentTuomas Ahola, May 12, 2026
  8. Junio C HamanoMay 12, 2026
  9. Jeff KingMay 12, 2026
  10. 2/3 approxidate: make "specials" respect fixed day-of-monthTuomas Ahola, May 12, 2026
  11. Junio C HamanoMay 12, 2026
  12. 3/3 approxidate: use deferred mday adjustments for "specials"Tuomas Ahola, May 12, 2026
  13. 0/4 approxidate: tweak special date formatsTuomas Ahola, May 14, 2026
  14. 2/4 approxidate: alias "today" to "now"Tuomas Ahola, May 14, 2026
  15. Junio C HamanoMay 14, 2026
  16. Tuomas AholaMay 14, 2026
  17. Junio C HamanoMay 15, 2026
  18. Junio C HamanoMay 15, 2026
  19. Tuomas AholaMay 15, 2026
  20. 1/4 t0006: add support for approxidate test date adjustmentTuomas Ahola, May 14, 2026
  21. 3/4 approxidate: make "specials" respect fixed day-of-monthTuomas Ahola, May 14, 2026
  22. Junio C HamanoMay 14, 2026
  23. 4/4 approxidate: use deferred mday adjustments for "specials"Tuomas Ahola, May 14, 2026
  24. 0/4 approxidate: tweak special date formatsTuomas Ahola, May 16, 2026
  25. 4/4 approxidate: use deferred mday adjustments for "specials"Tuomas Ahola, May 16, 2026
  26. fixup! approxidate: use deferred mday adjustments for "specials"Tuomas Ahola, May 18, 2026
  27. 2/4 t0006: add support for approxidate test date adjustmentTuomas Ahola, May 16, 2026
  28. 1/4 approxidate: make "today" wrap to midnightTuomas Ahola, May 16, 2026
  29. 3/4 approxidate: make "specials" respect fixed day-of-monthTuomas Ahola, May 16, 2026
  30. 0/4 approxidate: tweak special date formatsTuomas Ahola, May 21, 2026
  31. 1/4 approxidate: make "today" wrap to midnightTuomas Ahola, May 21, 2026
  32. 3/4 approxidate: make "specials" respect fixed day-of-monthTuomas Ahola, May 21, 2026
  33. 4/4 approxidate: use deferred mday adjustments for "specials"Tuomas Ahola, May 21, 2026
  34. 2/4 t0006: add support for approxidate test date adjustmentTuomas Ahola, May 21, 2026
  35. Junio C HamanoMay 21, 2026
  36. Tuomas AholaMay 21, 2026

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.