Skip to contentSkip to conversation

approxidate: tweak special date formats

PATCH In review Tuomas Ahola 3 participants 36 messages Mar 18, 2025 – May 21 Documentation

lore
Share & tools
lore
#0 lore raw

cover approxidate: tweak special date formats

I made a couple of somewhat hacky fixes in approxidate special date formats after noticing that some tests succeeded only because of the specified test time which happened to be too late to spot some irregularities.

Thanks a lot in advance!

Tuomas Ahola (2):
approxidate: make "specials" respect fixed day-of-month
approxidate: overwrite tm_mday for now and yesterday

 date.c          | 5 ++++-
 t/t0006-date.sh | 9 ++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

base-commit: 683c54c999c301c2cd6f715c411407c413b1d84e

Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 approxidate: make "specials" respect fixed day-of-month

The behaviour of noon and tea depends on the current time even when the date is given. In other words, "last Friday tea" is dated to Thursday if the command is run before 17 pm.

This can be fixed by checking whether tm->tm_mday already holds a determined value and tested by setting current time before 12 or 17 pm for noon and tea respectively.

---
 date.c          | 2 +-
 t/t0006-date.sh | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)
date.c +1 -1
index 17a95077cf..482a2f8c99 100644
@@ -1133,7 +1133,7 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
 	if (tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		update_tm(tm, now, tm->tm_mday < 0 ? 24*60*60 : 0);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;
t/t0006-date.sh +4 -1
index 53ced36df4..5db4b23e0b 100755
@@ -180,7 +180,10 @@ 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'
-check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+(
+	GIT_TEST_DATE_NOW=$(($GIT_TEST_DATE_NOW-12*60*60)); export GIT_TEST_DATE_NOW
+	check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+)
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] approxidate: make "specials" respect fixed day-of-month

Quoted 3 lines · Tuomas Ahola

The behaviour of noon and tea depends on the current time even when the date is given. In other words, "last Friday tea" is dated to Thursday if the command is run before 17 pm.

Knowing what approxidate's code is like, I'm not too surprised we'd have corner cases like this. ;)

Quoted 3 lines

This can be fixed by checking whether tm->tm_mday already holds a determined value and tested by setting current time before 12 or 17 pm for noon and tea respectively.

That makes sense for "last Friday tea", but should "tea last Friday" or "noon last Friday" work, too? I suspect it plays quite badly with approxidate's left-to-right parsing, so it might not be worth crossing that bridge.

date.c static void date_yesterday(struct tm *tm, struct tm *now, int *num) · line 1139

Quoted 11 lines
date.c +1 -1
@@ -1133,7 +1133,7 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
 	if (tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		update_tm(tm, now, tm->tm_mday < 0 ? 24*60*60 : 0);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;

My reading of that conditional is "if the time computed so far is before the specified hour, then go back a day to yesterday's version of it". So "noon" would be yesterday's noon if it is only 11am.

But if we already have a date, I'd think we would skip that logic entirely. I.e., do we need to call update_tm() at all? Certainly in your patch we'd pass 0, which would mean no adjustment. Do we need any other parts of update_tm()? It looks like it fills the month, day, and year from the "now" struct if they aren't already set, but presumably they'd all be set together (and if they're not, then that raises even more questions about whether just checking tm_mday is correct in your patch).

So it seems like:

/*
* If we do not yet have a specified day, we'll use the most recent
* version of "hour" relative to now. But that may be yesterday.
*/
if (tm->tm_mday < 0 && tm->tm_hour < hour)

	update_tm(tm, now, 24*60*60);

would be equivalent and is IMHO a bit easier to understand.

Quoted 5 lines
t/t0006-date.sh +0 -0
index 53ced36df4..5db4b23e0b 100755

t/t0006-date.sh check_approxidate '3:00' '2009-08-30 03:00:00' · line 187

Quoted 9 lines
t/t0006-date.sh +4 -1
@@ -180,7 +180,10 @@ 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'
-check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+(
+	GIT_TEST_DATE_NOW=$(($GIT_TEST_DATE_NOW-12*60*60)); export GIT_TEST_DATE_NOW
+	check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+)
 check_approxidate '10am noon' '2009-08-29 12:00:00'

I'm glad there's a test, but two comments. One, wouldn't we still want to keep the existing test to make sure that we continue to do the right thing when the "now" hour is after noon?

And two, I'm pretty sure this sub-shell will confuse the test harness, because you're running test_expect_success inside it. So any variable updates won't be seen by the parent shell, and I wouldn't be surprised if attempts to exit from "-i", etc, are broken.

Hmm, yeah, running the test yields this output:

ok 110 - parse approxidate (noon yesterday) ok 111 - parse approxidate (January 5th noon pm) ok 111 - parse approxidate (10am noon) ok 112 - parse approxidate (last tuesday)

because the update of the test counter is lost in the sub-shell.

I don't think there's a trivial solution. You can't do a one-shot variable-set like:

TEST_DATE_NOW=... check_approxidate ...

because the behavior of that construct varies between shells. So you have to either add support for an extra parameter to check_approxidate, or just save and restore like:

  old_date=$GIT_TEST_DATE_NOW
  GIT_TEST_DATE_NOW=$((GIT_TEST_DATE_NOW-12*60*60))
  check_approxidate ...
  GIT_TEST_DATE_NOW=$old_date

It would be nice to add a comment there, too, on what the 12-hour parameter means and what we expect. Maybe something like:

# The "now" date is usually at 1900 in the evening. Roll it back to

  # the morning so that it is before the hour of named times like
  # "noon" and "tea".

-Peff

#3 lore raw

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

In reply to Tuomas Ahola

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.

---
 date.c          | 3 +++
 t/t0006-date.sh | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)
date.c +3 -0
index 482a2f8c99..2a8a942d64 100644
@@ -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);
 }
 
 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);
 }
 
@@ -1204,6 +1206,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_now },
 	{ NULL }
 };
 
t/t0006-date.sh +2 -2
index 5db4b23e0b..6ad931dfb3 100755
@@ -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'
 )
 check_approxidate '10am noon' '2009-08-29 12:00:00'
Signature · 1 line
2.30.2
#4 lore raw

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

Quoted 4 lines · Tuomas Ahola

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).

date.c static void pending_number(struct tm *tm, int *num) · line 1126

Quoted 11 lines
date.c +1 -0
index 482a2f8c99..2a8a942d64 100644
@@ -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).

date.c static void pending_number(struct tm *tm, int *num) · line 1133

Quoted 6 lines
date.c +1 -0
 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.

date.c static const struct special { · line 1211

Quoted 7 lines
date.c +1 -0
@@ -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).

Quoted 4 lines
t/t0006-date.sh +0 -0
@@ -178,10 +178,10 @@ check_approxidate '6am yesterday' '2009-08-29 06:00:00'
 check_approxidate '6pm yesterday' '2009-08-29 18:00:00'

t/t0006-date.sh check_approxidate '6am yesterday' '2009-08-29 06:00:00' · line 186

Quoted 10 lines
t/t0006-date.sh +2 -2
 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

#5 lore raw

v2 cover approxidate: tweak special date formats

In reply to Tuomas Ahola

The approxidate system is an endless source of absurdities. Let's make the usual "eh, that's crazy, let's do better with this input" type of fix[1], and tweak some sharp edge cases, including one noticed by Linus back in 2006[2].

After this series, "tea" and "noon" will work predictably with all kinds of date formats (today, yesterday, last Friday, January 5th, one year ago yesterday...) regardless of the current time of day.

Links:
1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

Tuomas Ahola (3):
t0006: add support for approxidate test date adjustment
approxidate: make "specials" respect fixed day-of-month
approxidate: use deferred mday adjustments for "specials"

 date.c          | 37 +++++++++++++++++++++++++++----------
 t/t0006-date.sh | 19 ++++++++++++++++++-
 2 files changed, 45 insertions(+), 11 deletions(-)

Intervall-diff mot v1:

1:  9bfff739fd < -:  ---------- approxidate: make "specials" respect fixed day-of-month
2:  1a4398e5a1 < -:  ---------- approxidate: overwrite tm_mday for `now` and `yesterday`
-:  ---------- > 1:  118f1825ac t0006: add support for approxidate test date adjustment
-:  ---------- > 2:  21c4858c47 approxidate: make "specials" respect fixed day-of-month
-:  ---------- > 3:  cf72403102 approxidate: use deferred mday adjustments for "specials"

base-commit: c44beea485f0f2feaf460e2ac87fdd5608d63cf0

Signature · 1 line
2.30.2
#6 lore raw

PATCH v2 1/3 t0006: add support for approxidate test date adjustment

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test.

---
 t/t0006-date.sh | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
t/t0006-date.sh +10 -1
index 53ced36df4..5d66267672 100755
@@ -156,11 +156,18 @@ check_parse '2100-00-00 00:00:00 +11' bad
 REQUIRE_64BIT_TIME=
 
 check_approxidate() {
+	old_date=$GIT_TEST_DATE_NOW
+	GIT_TEST_DATE_NOW=$(($old_date${4:-+0}*60*60)); export GIT_TEST_DATE_NOW
+	caption=$1
+	if [ ! -z $4 ]; then
+		caption="$caption; offset $4h"
+	fi
 	echo "$1 -> $2 +0000" >expect
-	test_expect_${3:-success} "parse approxidate ($1)" "
+	test_expect_${3:-success} "parse approxidate ($caption)" "
 	test-tool date approxidate '$1' >actual &&
 	test_cmp expect actual
 	"
+	GIT_TEST_DATE_NOW=$old_date; export GIT_TEST_DATE_NOW
 }
 
 check_approxidate now '2009-08-30 19:20:00'
@@ -182,6 +189,8 @@ 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'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
+check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH v2 1/3] t0006: add support for approxidate test date adjustment

t/t0006-date.sh check_parse '2100-00-00 00:00:00 +11' bad · line 164

Quoted 7 lines · Tuomas Ahola

check_approxidate() {

t/t0006-date.sh +6 -0
+	old_date=$GIT_TEST_DATE_NOW
+	GIT_TEST_DATE_NOW=$(($old_date${4:-+0}*60*60)); export GIT_TEST_DATE_NOW
+	caption=$1
+	if [ ! -z $4 ]; then
+		caption="$caption; offset $4h"
+	fi

As you are not doing the test-date-now adjustment when $4 is not given, wouldn't it be a lot easier to read if you did something like

	old_date=$GIT_TEST_DATE_NOW
	if test -n "$4"
	then
		# the convention for $4 is a bit weird in that it 
		# comes with its own +/- operator in front.
		GIT_TEST_DATE_NOW=$(( $old_date $4 * 60 * 60 ))
		caption="$1; offset $4h"
	else
        	caption=$1
	fi

instead? Other two minor points are

- Documentation/SubmittingPatches prefers an explicit "test" over "[ ... ]", and have "then", "else", etc. on their own lines.

- As you never "unset" GIT_TEST_DATE_NOW, you do not have to keep exporting it. It is not like there are two variables (one for shell, the other for environment) and every time you set the shell one you need to export to reflect the value to the environment one. Rather, a single "export" marks a shell variable and every time it changes value, it is updated in the environment as well.

Quoted 19 lines
t/t0006-date.sh +4 -1
 	echo "$1 -> $2 +0000" >expect
-	test_expect_${3:-success} "parse approxidate ($1)" "
+	test_expect_${3:-success} "parse approxidate ($caption)" "
 	test-tool date approxidate '$1' >actual &&
 	test_cmp expect actual
 	"
+	GIT_TEST_DATE_NOW=$old_date; export GIT_TEST_DATE_NOW
 }
 
 check_approxidate now '2009-08-30 19:20:00'
@@ -182,6 +189,8 @@ 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'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
+check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
#8 lore raw

Re: [PATCH v2 1/3] t0006: add support for approxidate test date adjustment

In reply to Tuomas Ahola
Quoted 4 lines · Tuomas Ahola

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test.

Makes sense, but two small-ish comments:

Quoted 1 line

+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48

One, it sucks to have to say "success" here, but is awkward because now we have two optional arguments. There's nobody passing "failure" right now, so we could just drop support, though that might be annoying later when somebody wants to add a failing test. But we could perhaps switch to allowing:

  check_approxidate --failure 'January 5th yesterday' ...etc

which is fairly natural.

This is something we've run into in many different test scripts, and I think the harness could do a better job of supporting this. Perhaps with something like:

  test_expect() {
	if "$GIT_TEST_EXPECT" = "fail"
	then
		test_expect_failure "$@"
	else
		test_expect_success "$@"

}

  test_fails() {
	# probably needs to be more careful of one-shot with functions
	GIT_TEST_EXPECT=fail "$@"

}

  # this is a normal passing test snippet
  test_expect 'foo' 'git foo'
  # this one expects failure, but it can be toggled easily by
  # removing the leading "test_fails" wrapper. Not much better
  # than swapping out s/failure/success/ now. But...
  test_fails test_expect 'bar' 'git bar'
  # this one just does the right thing if the helper function
  # is using test_expect under the hood
  test_fails check_approxidate ...

I dunno. It is probably too big a rabbit hole to do before your series, so mostly I'm just thinking out loud.

Quoted 1 line

+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48

The second thing is that "+48" is pretty opaque. It's a relative offset to some arbitrary point. To some degree the script already suffers from that (all of the tests are using some arbitrary point), but I think the offset (without units!) adds a layer of indirection that makes it even more confusing.

I wonder how hard it would be to just take an arbitrary time instead, and then you could write:

check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '2009-08-28 12:00:00'

or whatever. There is a chicken-and-egg problem with testing our date routines and using the date routines to parse out the starting point. But I think for approxidate, we could be using the strict parser (tested separately earlier via check_parse) to handle the base time.

-Peff

#9 lore raw

PATCH v2 2/3 approxidate: make "specials" respect fixed day-of-month

In reply to Tuomas Ahola

The special approxidate time formats, "noon" and "tea", wrap to the previous day if the current time is before 12 or 5 pm, respectively. That holds even when an actual date is supplied; therefore, "10 May at tea" and "last Friday at noon" can cause the date to be set to a seemingly wrong day:

	now -> 2026-05-12 11:00:00 +0000
	10 May at tea -> 2026-05-09 17:00:00 +0000
	last Friday at noon -> 2026-05-07 12:00:00 +0000
	One year ago yesterday at tea-time -> 2025-05-10 17:00:00 +0000

The last example is from Linus Torvalds who remarked in 2006 that the answer was "just silly and not even correct." [1]

As "last Friday at noon" is mentioned in the documentation (date-formats.adoc) it would be nice if it worked correctly. Let's fix the glitch with a simple patch.

Check whether we already have a specified (non-negative) mday and make date_time() stick to it. Add a suitable time offset to the relevant test.

While we are at it, add "today" as an alias of "now", so that "today at noon" will do the right thing, too, and assert that with a new test.

Links:
1. https://lore.kernel.org/git/[email protected]/

---
 date.c          | 8 +++++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
date.c +7 -1
index 17a95077cf..e48cc2a4d7 100644
@@ -1132,7 +1132,12 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * By default, "tea" and "noon" refer to last such time in the
+	 * past, be it today or yesterday.  With a specified mday,
+	 * that logic is overridden.
+	 */
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
@@ -1204,6 +1209,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_now },
 	{ NULL }
 };
 
t/t0006-date.sh +4 -0
index 5d66267672..e01d093514 100755
@@ -186,8 +186,12 @@ 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 'today at noon' '2009-08-30 12:00:00' success -12
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' success -12
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success -12
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48
Signature · 1 line
2.30.2
#10 lore raw

Re: [PATCH v2 2/3] approxidate: make "specials" respect fixed day-of-month

Quoted 9 lines · Tuomas Ahola

The special approxidate time formats, "noon" and "tea", wrap to the previous day if the current time is before 12 or 5 pm, respectively. That holds even when an actual date is supplied; therefore, "10 May at tea" and "last Friday at noon" can cause the date to be set to a seemingly wrong day:

	now -> 2026-05-12 11:00:00 +0000
	10 May at tea -> 2026-05-09 17:00:00 +0000
	last Friday at noon -> 2026-05-07 12:00:00 +0000

It would help readers to say that Friday of that week was May 8th to make it easier for them to see why this is a wrong answer.

Quoted 4 lines
	One year ago yesterday at tea-time -> 2025-05-10 17:00:00 +0000

The last example is from Linus Torvalds who remarked in 2006 that the answer was "just silly and not even correct." [1]

It may be just me, but it was hard for me to guess if you are justifying how these answers are correct, or you are presenting examples of wrong output. Perhaps starting the paragraph with cases where the "wrap to the previous" gets right to set the stage may make it easier to understand? Let's see...

The ... 'noon' and 'tea', wrap to the ... before 12 or 5 pm, respectively. So for example if it is 11am on 2026-05-12, then

now -> 2026-05-12 11:00:00 +0000
noon -> 2026-05-11 11:00:00 +0000
tea -> 2026-05-11 17:00:00 +0000

which would work well when you ask for "git log --since=tea", for example.

That hold even when ... seemingly wrong day:

Quoted 26 lines

As "last Friday at noon" is mentioned in the documentation (date-formats.adoc) it would be nice if it worked correctly. Let's fix the glitch with a simple patch.

Check whether we already have a specified (non-negative) mday and make date_time() stick to it. Add a suitable time offset to the relevant test.

While we are at it, add "today" as an alias of "now", so that "today at noon" will do the right thing, too, and assert that with a new test.

Links:
1. https://lore.kernel.org/git/[email protected]/

---
 date.c          | 8 +++++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
date.c +0 -0
index 17a95077cf..e48cc2a4d7 100644

date.c static void date_yesterday(struct tm *tm, struct tm *now, int *num) · line 1139

Quoted 9 lines
date.c +5 -1
@@ -1132,7 +1132,12 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * By default, "tea" and "noon" refer to last such time in the
+	 * past, be it today or yesterday.  With a specified mday,
+	 * that logic is overridden.
+	 */

Again, this may be just me, but I happen to find the version of comment in Peff's review on the earlier iteration of this series much easier to understand.

date.c static const struct special { · line 1212

Quoted 9 lines
date.c +2 -0
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
@@ -1204,6 +1209,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_now },

Hmph, this may not work very well for "git log --since=today", which you may want to stop immediately when the traversal reaches a patch written before the most recent midnight, instead of stopping without giving anything back.

Quoted 20 lines
date.c +0 -0
 	{ NULL }
 };
 
t/t0006-date.sh +4 -0
index 5d66267672..e01d093514 100755
@@ -186,8 +186,12 @@ 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 'today at noon' '2009-08-30 12:00:00' success -12
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' success -12
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success -12
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' success +48
#11 lore raw

PATCH v2 3/3 approxidate: use deferred mday adjustments for "specials"

In reply to Tuomas Ahola

There are cases where the "wrap-to-yesterday" behavior of "tea" and "noon" should be reverted later on down the line, so that "today tea" and "tea today" won't yield different results. However, the logic of approxidate doesn't seem to lend itself particularly well to such cases.

Start tackling the issue by reusing negative values of tm->tm_mday field for deferred date adjustments which can be easily reverted, so that the default logic of the special formats only applies if we don't get any explicit date (mday) specification. In particular, overwrite the field with -1 in "now" and "yesterday", so that those formats will be relative to the current date. That makes specifications like "tea yesterday" behave more sensibly: instead of going backwards to the last tea-time and then a day back, Git will now understand that as the tea-time of yesterday.

Replace the call of update_tm() in date_time() with the assignment tm->tm_mday = -2. Add the corresponding code to handle that in update_tm(), wrapping to the previous day if the field still holds such assignment, meaning that we haven't seen any better specification for the day-of-month. On the other hand, mday=-3 would mean going two days back and so on. Even though such functionality isn't actually needed by this patch, it won't add much complexity in the code and is rather natural way to handle such values.

As date_time() won't no longer need the now struct, mark the associated function parameters as unused. The parameters themselves have to stay, however, as those functions are called through pointers in approxidate_alpha. Add relevant tests to cover the changes.

---
 date.c          | 31 +++++++++++++++++++++----------
 t/t0006-date.sh |  4 ++++
 2 files changed, 25 insertions(+), 10 deletions(-)
date.c +21 -10
index e48cc2a4d7..795f1ac2e9 100644
@@ -1071,13 +1071,22 @@ void datestamp(struct strbuf *out)
 /*
  * Relative time update (eg "2 days ago").  If we haven't set the time
  * yet, we need to set it from current time.
+ *
+ * The tm->tm_mday field has an additional logic of using negative values
+ * for date adjustments: -2 means yesterday and -3 the day before that,
+ * and so on.  The idea is to deref such adjustments until we are sure
+ * there's no explicit mday specification in the approxidate string.
  */
 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
 {
 	time_t n;
 
-	if (tm->tm_mday < 0)
+	if (tm->tm_mday < 0) {
+		int offset = tm->tm_mday + 1;
+		if (sec == 0 && offset < 0)
+			sec = -offset * 24*60*60;
 		tm->tm_mday = now->tm_mday;
+	}
 	if (tm->tm_mon < 0)
 		tm->tm_mon = now->tm_mon;
 	if (tm->tm_year < 0) {
@@ -1121,45 +1130,47 @@ 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);
 }
 
 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);
 }
 
-static void date_time(struct tm *tm, struct tm *now, int hour)
+static void date_time(struct tm *tm, int hour)
 {
 	/*
 	 * By default, "tea" and "noon" refer to last such time in the
 	 * past, be it today or yesterday.  With a specified mday,
-	 * that logic is overridden.
+	 * or e.g. "noon today", that logic is overridden.
 	 */
 	if (tm->tm_mday < 0 && tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		tm->tm_mday = -2; /* eventually handled by update_tm() */
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;
 }
 
-static void date_midnight(struct tm *tm, struct tm *now, int *num)
+static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 0);
+	date_time(tm, 0);
 }
 
-static void date_noon(struct tm *tm, struct tm *now, int *num)
+static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 12);
+	date_time(tm, 12);
 }
 
-static void date_tea(struct tm *tm, struct tm *now, int *num)
+static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 17);
+	date_time(tm, 17);
 }
 
 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
t/t0006-date.sh +4 -0
index e01d093514..2670269bb7 100755
@@ -187,9 +187,13 @@ 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 'today at noon' '2009-08-30 12:00:00' success -12
+check_approxidate 'noon today' '2009-09-01 12:00:00' success +36
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' success -12
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' success -12
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00' success -12
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success -12
 check_approxidate '10am noon' '2009-08-29 12:00:00'
Signature · 1 line
2.30.2
#12 lore raw

v3 cover approxidate: tweak special date formats

In reply to Tuomas Ahola

The approxidate system is an endless source of absurdities. Let's make the usual "eh, that's crazy, let's do better with this input" type of fix[1], and tweak some sharp edge cases, including one noticed by Linus back in 2006[2].

After this series, "tea" and "noon" will work predictably with all kinds of date formats (today, yesterday, last Friday, January 5th, one year ago yesterday...) regardless of the current time of day.

Links:
1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/

Tuomas Ahola (4):
t0006: add support for approxidate test date adjustment
approxidate: alias "today" to "now"
approxidate: make "specials" respect fixed day-of-month
approxidate: use deferred mday adjustments for "specials"

 date.c          | 36 ++++++++++++++++++++++++++----------
 t/t0006-date.sh | 41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 66 insertions(+), 11 deletions(-)

Intervall-diff mot v2:

1:  118f1825ac < -:  ---------- t0006: add support for approxidate test date adjustment
2:  21c4858c47 < -:  ---------- approxidate: make "specials" respect fixed day-of-month
-:  ---------- > 1:  7ea9c9967b t0006: add support for approxidate test date adjustment
-:  ---------- > 2:  3a21727dbe approxidate: alias "today" to "now"
-:  ---------- > 3:  d1992d23d0 approxidate: make "specials" respect fixed day-of-month
3:  cf72403102 ! 4:  0b1a10305c approxidate: use deferred mday adjustments for "specials"
    @@ date.c: static void pending_number(struct tm *tm, int *num)
     +static void date_time(struct tm *tm, int hour)
      {
      	/*
    - 	 * By default, "tea" and "noon" refer to last such time in the
    - 	 * past, be it today or yesterday.  With a specified mday,
    --	 * that logic is overridden.
    -+	 * or e.g. "noon today", that logic is overridden.
    + 	 * If we do not yet have a specified day, we'll use the most recent
    + 	 * version of "hour" relative to now.  But that may be yesterday.
      	 */
      	if (tm->tm_mday < 0 && tm->tm_hour < hour)
     -		update_tm(tm, now, 24*60*60);
    @@ t/t0006-date.sh
     @@ t/t0006-date.sh: 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 'today at noon' '2009-08-30 12:00:00' success -12
    -+check_approxidate 'noon today' '2009-09-01 12:00:00' success +36
    + check_approxidate 'today at noon' '2009-08-30 12:00:00' '-12 hours'
    ++check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
      check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
      check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
    - check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' success -12
    -+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' success -12
    + check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
    ++check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
     +check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
    -+check_approxidate 'tea last saturday' '2009-08-29 17:00:00' success -12
    ++check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
      check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
    - check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success -12
    + check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
      check_approxidate '10am noon' '2009-08-29 12:00:00'

base-commit: c44beea485f0f2feaf460e2ac87fdd5608d63cf0

Signature · 1 line
2.30.2
author Applied
#13 lore raw

PATCH v3 1/4 t0006: add support for approxidate test date adjustment

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test.

---

Notes:

Quoted 19 lines

As you are not doing the test-date-now adjustment when $4 is not given, wouldn't it be a lot easier to read if you did something like

	old_date=$GIT_TEST_DATE_NOW
	if test -n "$4"
	then
		# the convention for $4 is a bit weird in that it
		# comes with its own +/- operator in front.
		GIT_TEST_DATE_NOW=$(( $old_date $4 * 60 * 60 ))
		caption="$1; offset $4h"
	else
        	caption=$1
	fi

instead? Other two minor points are

- Documentation/SubmittingPatches prefers an explicit "test" over "[ ... ]", and have "then", "else", etc. on their own lines.

Quoted 8 lines

- As you never "unset" GIT_TEST_DATE_NOW, you do not have to keep exporting it. It is not like there are two variables (one for shell, the other for environment) and every time you set the shell one you need to export to reflect the value to the environment one. Rather, a single "export" marks a shell variable and every time it changes value, it is updated in the environment as well.

Thanks, applied.

> One, it sucks to have to say "success" here, but is awkward because now > we have two optional arguments. There's nobody passing "failure" right > now, so we could just drop support, though that might be annoying later > when somebody wants to add a failing test. But we could perhaps switch > to allowing:
>

Ok, now it works without "success" in between, and "failure" works without an offset, too.

check_approxidate <test-string> <expected-result> [<test-time-offset>] [failure]

> The second thing is that "+48" is pretty opaque. It's a relative offset > to some arbitrary point. To some degree the script already suffers from > that (all of the tests are using some arbitrary point), but I think the > offset (without units!) adds a layer of indirection that makes it even > more confusing.
>

Good catch. Now it is at least marginally better with the added units.

 t/t0006-date.sh | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
t/t0006-date.sh +32 -1
index 53ced36df4..c7667bade2 100755
@@ -155,12 +155,41 @@ check_parse '2100-00-00 00:00:00 -11' bad
 check_parse '2100-00-00 00:00:00 +11' bad
 REQUIRE_64BIT_TIME=
 
+add_time_offset() {
+	case "$3" in
+	hours)
+		unit=$(( 60*60 ))
+		;;
+	days)
+		unit=$(( 24*60*60 ))
+		;;
+	esac
+	offset=$(( $2 * unit ))
+	echo $(( $1 + offset ))
+}
+
 check_approxidate() {
+	old_date=$GIT_TEST_DATE_NOW
+	if test "$3" = "failure"
+	then
+		expection="$3"
+	else
+		expection=${4:-success}
+		offset="$3"
+	fi
+	if test -n "$offset"
+	then
+		GIT_TEST_DATE_NOW=$(add_time_offset $old_date $offset)
+		caption="$1; offset $offset"
+	else
+		caption=$1
+	fi
 	echo "$1 -> $2 +0000" >expect
-	test_expect_${3:-success} "parse approxidate ($1)" "
+	test_expect_$expection "parse approxidate ($caption)" "
 	test-tool date approxidate '$1' >actual &&
 	test_cmp expect actual
 	"
+	GIT_TEST_DATE_NOW=$old_date
 }
 
 check_approxidate now '2009-08-30 19:20:00'
@@ -182,6 +211,8 @@ 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'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
+check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
Signature · 1 line
2.30.2
#14 lore raw

PATCH v3 2/4 approxidate: alias "today" to "now"

In reply to Tuomas Ahola

As far as approxidate in concerned, "today" is a no-op. That makes it functionally equivalent to "now" in commands like

        $ git log --since=today

Make that behavior explicit by binding "today" to date_now(). That way later patches can give "today" some functionality in cases like "today at noon".

---

Notes:

Quoted 5 lines

Hmph, this may not work very well for "git log --since=today", which you may want to stop immediately when the traversal reaches a patch written before the most recent midnight, instead of stopping without giving anything back.

    Sorry, I don't know if I understood.  Does the patch change the behavior of
    that command somehow?  Is there some kind of edge case I missed?
    
    That said, if we do want to change it so that "git log --since=today"
    worked like "--since=midnight", this seems to do the trick:
    
    ```
    static void date_today(struct tm *tm, struct tm *now, int *num)
    {
    	if (tm->tm_hour == now->tm_hour &&
    	    tm->tm_min == now->tm_min &&
    	    tm->tm_sec == now->tm_sec)
    		date_midnight(tm, now, num);
    	date_now(tm, now, num);
    }
    ```
 date.c | 1 +
 1 file changed, 1 insertion(+)
date.c +1 -0
index 17a95077cf..412aca6dc4 100644
@@ -1204,6 +1204,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_now },
 	{ NULL }
 };
 
Signature · 1 line
2.30.2
#15 lore raw

Re: [PATCH v3 2/4] approxidate: alias "today" to "now"

Quoted 2 lines · Tuomas Ahola

Sorry, I don't know if I understood. Does the patch change the behavior of that command somehow? Is there some kind of edge case I missed?

No, I did not think it was a good idea to carve the behaviour in stone that "git log --since=today" behaves as if it were given "git log --since=now". My reaction would have been very different if we were deliberatly and explicitly saying "today is synonym for now", but the thing is, it is not a designed behaviour but what approxidate does for anything it does not understand, e.g.

    git log --since=decay
    git log --since=bogus

all behave as if it were given --since=now.

#16 lore raw

Re: [PATCH v3 2/4] approxidate: alias "today" to "now"

Quoted 12 lines · Junio C Hamano
Quoted 2 lines · Tuomas Ahola

Sorry, I don't know if I understood. Does the patch change the behavior of that command somehow? Is there some kind of edge case I missed?

No, I did not think it was a good idea to carve the behaviour in stone that "git log --since=today" behaves as if it were given "git log --since=now". My reaction would have been very different if we were deliberatly and explicitly saying "today is synonym for now", but the thing is, it is not a designed behaviour but what approxidate does for anything it does not understand, e.g.

Quoted 4 lines
    git log --since=decay
    git log --since=bogus

all behave as if it were given --since=now.

Thanks for spelling that out. So, as there is no deliberative decision behind the current behaviour of "today", the code has to remain non-committed on that; we are not at liberty to codify the status quo. Right? But perhaps we can find a least common denominator. "Today" means the current day (well, obviously), and this seems to be enough to get "today at noon" and such to work:

```
static void date_today(struct tm *tm, struct tm *now, int *num)
{
	*num = 0;
	tm->tm_mday = now->tm_mday;
}
```

It gets the job done whitout setting in stone too much anything that is up for debate. Do you see that could work?

--Tuomas

#17 lore raw

Re: [PATCH v3 2/4] approxidate: alias "today" to "now"

Quoted 17 lines · Tuomas Ahola
Quoted 15 lines · Junio C Hamano
Quoted 2 lines · Tuomas Ahola

Sorry, I don't know if I understood. Does the patch change the behavior of that command somehow? Is there some kind of edge case I missed?

No, I did not think it was a good idea to carve the behaviour in stone that "git log --since=today" behaves as if it were given "git log --since=now". My reaction would have been very different if we were deliberatly and explicitly saying "today is synonym for now", but the thing is, it is not a designed behaviour but what approxidate does for anything it does not understand, e.g.

    git log --since=decay
    git log --since=bogus
Quoted 6 lines
Quoted 1 line

all behave as if it were given --since=now.

Thanks for spelling that out. So, as there is no deliberative decision behind the current behaviour of "today", the code has to remain non-committed on that; we are not at liberty to codify the status quo. Right?

Not right. It is more like "Even though we try not to change existing behavoiur left and right without a good reason to avoid breaking existing users' expectations, we should be able to "fix" what is not intended behaviour but is something the code happened to be doing, especially if the current behaviour does not make sense.

#18 lore raw

Re: [PATCH v3 2/4] approxidate: alias "today" to "now"

Quoted 26 lines · Junio C Hamano
Quoted 17 lines · Tuomas Ahola
Quoted 15 lines · Junio C Hamano
Quoted 2 lines · Tuomas Ahola

Sorry, I don't know if I understood. Does the patch change the behavior of that command somehow? Is there some kind of edge case I missed?

No, I did not think it was a good idea to carve the behaviour in stone that "git log --since=today" behaves as if it were given "git log --since=now". My reaction would have been very different if we were deliberatly and explicitly saying "today is synonym for now", but the thing is, it is not a designed behaviour but what approxidate does for anything it does not understand, e.g.

    git log --since=decay
    git log --since=bogus
Quoted 6 lines
Quoted 1 line

all behave as if it were given --since=now.

Thanks for spelling that out. So, as there is no deliberative decision behind the current behaviour of "today", the code has to remain non-committed on that; we are not at liberty to codify the status quo. Right?

Quoted 5 lines

Not right. It is more like "Even though we try not to change existing behavoiur left and right without a good reason to avoid breaking existing users' expectations, we should be able to "fix" what is not intended behaviour but is something the code happened to be doing, especially if the current behaviour does not make sense.

And the other half of the discussion is that once we explicitly say "today means right now" and make it official, it makes it much harder to fix it later. So we need to be very careful in our first attempt.

#19 lore raw

Re: [PATCH v3 2/4] approxidate: alias "today" to "now"

Quoted 28 lines · Junio C Hamano
Quoted 26 lines · Junio C Hamano
Quoted 23 lines · Tuomas Ahola
Quoted 12 lines · Junio C Hamano
Quoted 2 lines · Tuomas Ahola

Sorry, I don't know if I understood. Does the patch change the behavior of that command somehow? Is there some kind of edge case I missed?

No, I did not think it was a good idea to carve the behaviour in stone that "git log --since=today" behaves as if it were given "git log --since=now". My reaction would have been very different if we were deliberatly and explicitly saying "today is synonym for now", but the thing is, it is not a designed behaviour but what approxidate does for anything it does not understand, e.g.

Quoted 4 lines
    git log --since=decay
    git log --since=bogus

all behave as if it were given --since=now.

Thanks for spelling that out. So, as there is no deliberative decision behind the current behaviour of "today", the code has to remain non-committed on that; we are not at liberty to codify the status quo. Right?

Quoted 5 lines
Quoted 5 lines

Not right. It is more like "Even though we try not to change existing behavoiur left and right without a good reason to avoid breaking existing users' expectations, we should be able to "fix" what is not intended behaviour but is something the code happened to be doing, especially if the current behaviour does not make sense.

Well, that's not far off from what I wrote. I just meant we cannot make the current (somewhat accidental) behaviour official *just because* it happens to be the status quo.

Quoted 4 lines

And the other half of the discussion is that once we explicitly say "today means right now" and make it official, it makes it much harder to fix it later. So we need to be very careful in our first attempt.

Roger that.

author Applied
#20 lore raw

PATCH v3 3/4 approxidate: make "specials" respect fixed day-of-month

In reply to Tuomas Ahola

The special approxidate time formats, "noon" and "tea" differ from "12pm" and "5pm" by having the feature of wrapping to the previous day if the current time is before those hours:

	now  -> 2026-05-13 11:00:00 +0000

	12pm -> 2026-05-13 12:00:00 +0000
	5pm  -> 2026-05-13 17:00:00 +0000

	noon -> 2026-05-12 12:00:00 +0000
	tea  -> 2026-05-12 17:00:00 +0000

However, that logic carries too far. Even when the date is specified, the behavior of the "specials" depends on the current time. Assuming the same time as above, we get:

	today at noon -> 2026-05-12 12:00:00 +0000 (should be 13 May)
	13 May at tea -> 2026-05-12 17:00:00 +0000

or, using an example mentioned in date-formats.adoc:

	last Friday at noon -> 2026-05-07 12:00:00 +0000 (should be 8 May)

The quirk seems to be rather old. Already in 2006, Linus Torvalds remarked that the date yielded by "one year ago yesterday at tea-time" was "just silly and not even correct". Indeed, even today it gives:

	One year ago yesterday at tea-time -> 2025-05-11 17:00:00 +0000
	  (should be 12 May)

Let's fix all of those with a simple patch. Check whether we already have a specified day-of-month in tm->tm_mday and make date_time() stick to it. Ensure the correct behavior with relevant tests.

Links:
1. https://lore.kernel.org/git/[email protected]/

---

Notes:

Quoted 4 lines

Again, this may be just me, but I happen to find the version of comment in Peff's review on the earlier iteration of this series much easier to understand.

Thanks, applied.

 date.c          | 6 +++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)
date.c +5 -1
index 412aca6dc4..73879d202c 100644
@@ -1132,7 +1132,11 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * If we do not yet have a specified day, we'll use the most recent
+	 * version of "hour" relative to now.  But that may be yesterday.
+	 */
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
t/t0006-date.sh +4 -0
index c7667bade2..d800cb30fe 100755
@@ -208,8 +208,12 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
Signature · 1 line
2.30.2
#21 lore raw

Re: [PATCH v3 3/4] approxidate: make "specials" respect fixed day-of-month

Quoted 27 lines · Tuomas Ahola

The special approxidate time formats, "noon" and "tea" differ from "12pm" and "5pm" by having the feature of wrapping to the previous day if the current time is before those hours:

	now  -> 2026-05-13 11:00:00 +0000

	12pm -> 2026-05-13 12:00:00 +0000
	5pm  -> 2026-05-13 17:00:00 +0000

	noon -> 2026-05-12 12:00:00 +0000
	tea  -> 2026-05-12 17:00:00 +0000

However, that logic carries too far. Even when the date is specified, the behavior of the "specials" depends on the current time. Assuming the same time as above, we get:

	today at noon -> 2026-05-12 12:00:00 +0000 (should be 13 May)
	13 May at tea -> 2026-05-12 17:00:00 +0000

or, using an example mentioned in date-formats.adoc:

	last Friday at noon -> 2026-05-07 12:00:00 +0000 (should be 8 May)

The quirk seems to be rather old. Already in 2006, Linus Torvalds remarked that the date yielded by "one year ago yesterday at tea-time" was "just silly and not even correct". Indeed, even today it gives:

Quoted 6 lines
	One year ago yesterday at tea-time -> 2025-05-11 17:00:00 +0000
	  (should be 12 May)

Let's fix all of those with a simple patch. Check whether we already have a specified day-of-month in tm->tm_mday and make date_time() stick to it. Ensure the correct behavior with relevant tests.

I find this vastly easier to follow the reasoning, compared to the previous iteration. Very nicely done.

Quoted 53 lines

Links:
1. https://lore.kernel.org/git/[email protected]/

---

Notes:

Quoted 4 lines

Again, this may be just me, but I happen to find the version of comment in Peff's review on the earlier iteration of this series much easier to understand.

Thanks, applied.

 date.c          | 6 +++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)
date.c +5 -1
index 412aca6dc4..73879d202c 100644
@@ -1132,7 +1132,11 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * If we do not yet have a specified day, we'll use the most recent
+	 * version of "hour" relative to now.  But that may be yesterday.
+	 */
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
t/t0006-date.sh +4 -0
index c7667bade2..d800cb30fe 100755
@@ -208,8 +208,12 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
#22 lore raw

PATCH v3 4/4 approxidate: use deferred mday adjustments for "specials"

In reply to Tuomas Ahola

There are cases where the "wrap-to-yesterday" behavior of "tea" and "noon" should be reverted later on down the line, so that "today tea" and "tea today" won't yield different results. However, the logic of approxidate doesn't seem to lend itself particularly well to such cases.

Start tackling the issue by reusing negative values of tm->tm_mday field for deferred date adjustments which can be easily reverted, so that the default logic of the special formats only applies if we don't get any explicit date (mday) specification. In particular, overwrite the field with -1 in "now" and "yesterday", so that those formats will be relative to the current date. That makes specifications like "tea yesterday" behave more sensibly: instead of going backwards to the last tea-time and then a day back, Git will now understand that as the tea-time of yesterday.

Replace the call of update_tm() in date_time() with the assignment tm->tm_mday = -2. Add the corresponding code to handle that in update_tm(), wrapping to the previous day if the field still holds such assignment, meaning that we haven't seen any better specification for the day-of-month. On the other hand, mday=-3 would mean going two days back and so on. Even though such functionality isn't actually needed by this patch, it won't add much complexity in the code and is rather natural way to handle such values.

As date_time() won't no longer need the now struct, mark the associated function parameters as unused. The parameters themselves have to stay, however, as those functions are called through pointers in approxidate_alpha. Add relevant tests to cover the changes.

---
 date.c          | 29 ++++++++++++++++++++---------
 t/t0006-date.sh |  4 ++++
 2 files changed, 24 insertions(+), 9 deletions(-)
date.c +20 -9
index 73879d202c..914c733737 100644
@@ -1071,13 +1071,22 @@ void datestamp(struct strbuf *out)
 /*
  * Relative time update (eg "2 days ago").  If we haven't set the time
  * yet, we need to set it from current time.
+ *
+ * The tm->tm_mday field has an additional logic of using negative values
+ * for date adjustments: -2 means yesterday and -3 the day before that,
+ * and so on.  The idea is to deref such adjustments until we are sure
+ * there's no explicit mday specification in the approxidate string.
  */
 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
 {
 	time_t n;
 
-	if (tm->tm_mday < 0)
+	if (tm->tm_mday < 0) {
+		int offset = tm->tm_mday + 1;
+		if (sec == 0 && offset < 0)
+			sec = -offset * 24*60*60;
 		tm->tm_mday = now->tm_mday;
+	}
 	if (tm->tm_mon < 0)
 		tm->tm_mon = now->tm_mon;
 	if (tm->tm_year < 0) {
@@ -1121,44 +1130,46 @@ 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);
 }
 
 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);
 }
 
-static void date_time(struct tm *tm, struct tm *now, int hour)
+static void date_time(struct tm *tm, int hour)
 {
 	/*
 	 * If we do not yet have a specified day, we'll use the most recent
 	 * version of "hour" relative to now.  But that may be yesterday.
 	 */
 	if (tm->tm_mday < 0 && tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		tm->tm_mday = -2; /* eventually handled by update_tm() */
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;
 }
 
-static void date_midnight(struct tm *tm, struct tm *now, int *num)
+static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 0);
+	date_time(tm, 0);
 }
 
-static void date_noon(struct tm *tm, struct tm *now, int *num)
+static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 12);
+	date_time(tm, 12);
 }
 
-static void date_tea(struct tm *tm, struct tm *now, int *num)
+static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 17);
+	date_time(tm, 17);
 }
 
 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
t/t0006-date.sh +4 -0
index d800cb30fe..432b92f841 100755
@@ -209,9 +209,13 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
+check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
Signature · 1 line
2.30.2
#23 lore raw

v4 cover approxidate: tweak special date formats

In reply to Tuomas Ahola

The approxidate system is an endless source of absurdities. Let's make the usual "eh, that's crazy, let's do better with this input" type of fix[1], and tweak some sharp edge cases, including one noticed by Linus back in 2006[2].

After this series, "tea" and "noon" will work predictably with all kinds of date formats (today, yesterday, last Friday, January 5th, one year ago yesterday...) regardless of the current time of day.

More importantly, approxidate is taught about "today", meaning by default the last midnight, as discussed in the RFC thread.[3]

Links:
1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/
3. https://lore.kernel.org/git/[email protected]/

Tuomas Ahola (4):
approxidate: make "today" wrap to midnight
t0006: add support for approxidate test date adjustment
approxidate: make "specials" respect fixed day-of-month
approxidate: use deferred mday adjustments for "specials"

 Documentation/rev-list-options.adoc |  3 +-
 date.c                              | 45 ++++++++++++++++++++++-------
 t/t0006-date.sh                     | 43 ++++++++++++++++++++++++++-
 3 files changed, 79 insertions(+), 12 deletions(-)

Intervall-diff mot v3:

-:  ---------- > 1:  86bcb70ac2 approxidate: make "today" wrap to midnight
1:  7ea9c9967b = 2:  9863f359a1 t0006: add support for approxidate test date adjustment
2:  3a21727dbe < -:  ---------- approxidate: alias "today" to "now"
3:  d1992d23d0 = 3:  830de02f74 approxidate: make "specials" respect fixed day-of-month
4:  0b1a10305c ! 4:  d33195dc91 approxidate: use deferred mday adjustments for "specials"

@@ Commit message
field for deferred date adjustments which can be easily reverted, so that the default logic of the special formats only applies if we don't get any explicit date (mday) specification. In particular, overwrite
- the field with -1 in "now" and "yesterday", so that those formats will
+ the field with -1 in "today" and "yesterday", so that those formats will
be relative to the current date. That makes specifications like "tea yesterday" behave more sensibly: instead of going backwards to the last tea-time and then a day back, Git will now understand that as the
@@ date.c: void datestamp(struct strbuf *out)
if (tm->tm_mon < 0)
tm->tm_mon = now->tm_mon;
if (tm->tm_year < 0) {
-@@ date.c: 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);
- }
-
+@@ date.c: static void date_now(struct tm *tm, struct tm *now, int *num)
static void date_yesterday(struct tm *tm, struct tm *now, int *num) {
*num = 0;
@@ date.c: static void pending_number(struct tm *tm, int *num)
}

static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
+@@ date.c: static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
+ if (tm->tm_hour == now->tm_hour &&
+ tm->tm_min == now->tm_min &&
+ tm->tm_sec == now->tm_sec)
+- date_time(tm, now, 0);
++ date_time(tm, 0);
++ tm->tm_mday = -1;
+ update_tm(tm, now, 0);
+ }
+

## t/t0006-date.sh ##
@@ t/t0006-date.sh: check_approxidate '3:00' '2009-08-30 03:00:00'

base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0

Signature · 1 line
2.30.2
#24 lore raw

PATCH v4 1/4 approxidate: make "today" wrap to midnight

Although some commands do reject invalid approxidate expressions, in other cases those are simply evaluated as the current time. Oftentimes that is a perfectly good compromise to handle silly requests, but it isn't without rough edges.

Because of the silent acceptance, it is easy to forget that "today" isn't actually a valid approxidate format. That is a bit awkward because while the fallback logic of using the current time does make some sense, there is no deliberative decision behind such behavior of "today". Indeed, whatever (non-)action "today" currently has, is just an accidental side effect.

That means "git log --since=today" is currently unlikely to print anything at all as it tries to list commits dated with *future* timestamps. Arguably it would be more useful to list the commits of the current day---i.e. those made since midnight.

On the other hand, "git log --until=today" doesn't really filter commits at all. Changing the definition of "today" would make it return the commits made before the current day. That isn't without problems though---running "git log --until=today" in the late afternoon could reasonably include the work done earlier that day (as the command currently does do).

Still the utility of no-op "--until=today" is debatable and perhaps outweighed by the pros of having "--since=today" to mean "--since=midnight". The thing is that the approxidate machinery doesn't know about its consumers, so the meaning of "today" has to be the same for "--since" and "--until".

In fact, "git log --until=" is documented as

	`--until=<date>`::
	`--before=<date>`::
		Show commits older than _<date>_,

so excluding commits made today would actually match the documentation more closely.

Moreover, a revision parameter "@{today}" is currently outright rejected. Making "today" a valid approxidate time format could make a natural way to specify the state of the ref at the start of the current day.

Bind "today" to new function date_today() as an approxidate special. Make it return the last midnight if no specific time is given; i.e. retain the old behavior of "noon today" and such.

Document the new behavior of "git log --since=today" in rev-list-options.adoc.

---
 Documentation/rev-list-options.adoc |  3 ++-
 date.c                              | 10 ++++++++++
 t/t0006-date.sh                     |  2 ++
 3 files changed, 14 insertions(+), 1 deletion(-)
Documentation/rev-list-options.adoc +2 -1
index 2d195a1474..a5abadf689 100644
@@ -23,7 +23,8 @@ ordering and formatting options, such as `--reverse`.
 
 `--since=<date>`::
 `--after=<date>`::
-	Show commits more recent than _<date>_.
+	Show commits more recent than _<date>_.  As a special case,
+	'today' means the last midnight.
 
 `--since-as-filter=<date>`::
 	Show all commits more recent than _<date>_. This visits
date.c +10 -0
index 17a95077cf..343d6aab6f 100644
@@ -1192,6 +1192,15 @@ static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
 	*num = 0;
 }
 
+static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
+{
+	if (tm->tm_hour == now->tm_hour &&
+	    tm->tm_min == now->tm_min &&
+	    tm->tm_sec == now->tm_sec)
+		date_time(tm, now, 0);
+	update_tm(tm, now, 0);
+}
+
 static const struct special {
 	const char *name;
 	void (*fn)(struct tm *, struct tm *, int *);
@@ -1204,6 +1213,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_today },
 	{ NULL }
 };
 
t/t0006-date.sh +2 -0
index 53ced36df4..07bf6115ab 100755
@@ -164,6 +164,7 @@ check_approxidate() {
 }
 
 check_approxidate now '2009-08-30 19:20:00'
+check_approxidate today '2009-08-30 00:00:00'
 check_approxidate '5 seconds ago' '2009-08-30 19:19:55'
 check_approxidate 5.seconds.ago '2009-08-30 19:19:55'
 check_approxidate 10.minutes.ago '2009-08-30 19:10:00'
@@ -187,6 +188,7 @@ check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
 check_approxidate '06/05/2009' '2009-06-05 19:20:00'
 check_approxidate '06.05.2009' '2009-05-06 19:20:00'
+check_approxidate 'Jan 5 today' '2009-01-30 00:00:00'
 
 check_approxidate 'Jun 6, 5AM' '2009-06-06 05:00:00'
 check_approxidate '5AM Jun 6' '2009-06-06 05:00:00'
Signature · 1 line
2.30.2
#25 lore raw

PATCH v4 2/4 t0006: add support for approxidate test date adjustment

In reply to Tuomas Ahola

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test.

---
 t/t0006-date.sh | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
t/t0006-date.sh +32 -1
index 07bf6115ab..15fbc12861 100755
@@ -155,12 +155,41 @@ check_parse '2100-00-00 00:00:00 -11' bad
 check_parse '2100-00-00 00:00:00 +11' bad
 REQUIRE_64BIT_TIME=
 
+add_time_offset() {
+	case "$3" in
+	hours)
+		unit=$(( 60*60 ))
+		;;
+	days)
+		unit=$(( 24*60*60 ))
+		;;
+	esac
+	offset=$(( $2 * unit ))
+	echo $(( $1 + offset ))
+}
+
 check_approxidate() {
+	old_date=$GIT_TEST_DATE_NOW
+	if test "$3" = "failure"
+	then
+		expection="$3"
+	else
+		expection=${4:-success}
+		offset="$3"
+	fi
+	if test -n "$offset"
+	then
+		GIT_TEST_DATE_NOW=$(add_time_offset $old_date $offset)
+		caption="$1; offset $offset"
+	else
+		caption=$1
+	fi
 	echo "$1 -> $2 +0000" >expect
-	test_expect_${3:-success} "parse approxidate ($1)" "
+	test_expect_$expection "parse approxidate ($caption)" "
 	test-tool date approxidate '$1' >actual &&
 	test_cmp expect actual
 	"
+	GIT_TEST_DATE_NOW=$old_date
 }
 
 check_approxidate now '2009-08-30 19:20:00'
@@ -183,6 +212,8 @@ 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'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
+check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
Signature · 1 line
2.30.2
#26 lore raw

PATCH v4 3/4 approxidate: make "specials" respect fixed day-of-month

In reply to Tuomas Ahola

The special approxidate time formats, "noon" and "tea" differ from "12pm" and "5pm" by having the feature of wrapping to the previous day if the current time is before those hours:

	now  -> 2026-05-13 11:00:00 +0000

	12pm -> 2026-05-13 12:00:00 +0000
	5pm  -> 2026-05-13 17:00:00 +0000

	noon -> 2026-05-12 12:00:00 +0000
	tea  -> 2026-05-12 17:00:00 +0000

However, that logic carries too far. Even when the date is specified, the behavior of the "specials" depends on the current time. Assuming the same time as above, we get:

	today at noon -> 2026-05-12 12:00:00 +0000 (should be 13 May)
	13 May at tea -> 2026-05-12 17:00:00 +0000

or, using an example mentioned in date-formats.adoc:

	last Friday at noon -> 2026-05-07 12:00:00 +0000 (should be 8 May)

The quirk seems to be rather old. Already in 2006, Linus Torvalds remarked that the date yielded by "one year ago yesterday at tea-time" was "just silly and not even correct". Indeed, even today it gives:

	One year ago yesterday at tea-time -> 2025-05-11 17:00:00 +0000
	  (should be 12 May)

Let's fix all of those with a simple patch. Check whether we already have a specified day-of-month in tm->tm_mday and make date_time() stick to it. Ensure the correct behavior with relevant tests.

Links:
1. https://lore.kernel.org/git/[email protected]/

---
 date.c          | 6 +++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)
date.c +5 -1
index 343d6aab6f..7a458f3cac 100644
@@ -1132,7 +1132,11 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * If we do not yet have a specified day, we'll use the most recent
+	 * version of "hour" relative to now.  But that may be yesterday.
+	 */
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
t/t0006-date.sh +4 -0
index 15fbc12861..7358903046 100755
@@ -209,8 +209,12 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
Signature · 1 line
2.30.2
#27 lore raw

PATCH v4 4/4 approxidate: use deferred mday adjustments for "specials"

In reply to Tuomas Ahola

There are cases where the "wrap-to-yesterday" behavior of "tea" and "noon" should be reverted later on down the line, so that "today tea" and "tea today" won't yield different results. However, the logic of approxidate doesn't seem to lend itself particularly well to such cases.

Start tackling the issue by reusing negative values of tm->tm_mday field for deferred date adjustments which can be easily reverted, so that the default logic of the special formats only applies if we don't get any explicit date (mday) specification. In particular, overwrite the field with -1 in "today" and "yesterday", so that those formats will be relative to the current date. That makes specifications like "tea yesterday" behave more sensibly: instead of going backwards to the last tea-time and then a day back, Git will now understand that as the tea-time of yesterday.

Replace the call of update_tm() in date_time() with the assignment tm->tm_mday = -2. Add the corresponding code to handle that in update_tm(), wrapping to the previous day if the field still holds such assignment, meaning that we haven't seen any better specification for the day-of-month. On the other hand, mday=-3 would mean going two days back and so on. Even though such functionality isn't actually needed by this patch, it won't add much complexity in the code and is rather natural way to handle such values.

As date_time() won't no longer need the now struct, mark the associated function parameters as unused. The parameters themselves have to stay, however, as those functions are called through pointers in approxidate_alpha. Add relevant tests to cover the changes.

---
 date.c          | 31 +++++++++++++++++++++----------
 t/t0006-date.sh |  4 ++++
 2 files changed, 25 insertions(+), 10 deletions(-)
date.c +21 -10
index 7a458f3cac..6e7cf907da 100644
@@ -1071,13 +1071,22 @@ void datestamp(struct strbuf *out)
 /*
  * Relative time update (eg "2 days ago").  If we haven't set the time
  * yet, we need to set it from current time.
+ *
+ * The tm->tm_mday field has an additional logic of using negative values
+ * for date adjustments: -2 means yesterday and -3 the day before that,
+ * and so on.  The idea is to deref such adjustments until we are sure
+ * there's no explicit mday specification in the approxidate string.
  */
 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
 {
 	time_t n;
 
-	if (tm->tm_mday < 0)
+	if (tm->tm_mday < 0) {
+		int offset = tm->tm_mday + 1;
+		if (sec == 0 && offset < 0)
+			sec = -offset * 24*60*60;
 		tm->tm_mday = now->tm_mday;
+	}
 	if (tm->tm_mon < 0)
 		tm->tm_mon = now->tm_mon;
 	if (tm->tm_year < 0) {
@@ -1127,38 +1136,39 @@ static void date_now(struct tm *tm, struct tm *now, int *num)
 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);
 }
 
-static void date_time(struct tm *tm, struct tm *now, int hour)
+static void date_time(struct tm *tm, int hour)
 {
 	/*
 	 * If we do not yet have a specified day, we'll use the most recent
 	 * version of "hour" relative to now.  But that may be yesterday.
 	 */
 	if (tm->tm_mday < 0 && tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		tm->tm_mday = -2; /* eventually handled by update_tm() */
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;
 }
 
-static void date_midnight(struct tm *tm, struct tm *now, int *num)
+static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 0);
+	date_time(tm, 0);
 }
 
-static void date_noon(struct tm *tm, struct tm *now, int *num)
+static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 12);
+	date_time(tm, 12);
 }
 
-static void date_tea(struct tm *tm, struct tm *now, int *num)
+static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 17);
+	date_time(tm, 17);
 }
 
 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
@@ -1201,7 +1211,8 @@ static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
 	if (tm->tm_hour == now->tm_hour &&
 	    tm->tm_min == now->tm_min &&
 	    tm->tm_sec == now->tm_sec)
-		date_time(tm, now, 0);
+		date_time(tm, 0);
+	tm->tm_mday = -1;
 	update_tm(tm, now, 0);
 }
 
t/t0006-date.sh +4 -0
index 7358903046..b187b1bfc4 100755
@@ -210,9 +210,13 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
+check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
Signature · 1 line
2.30.2
#28 lore raw

PATCH 1/1 fixup! approxidate: use deferred mday adjustments for "specials"

Oops, let's reorder these lines.

---
 t/t0006-date.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t0006-date.sh +1 -1
index b187b1bfc4..63e5628e05 100755
@@ -212,9 +212,9 @@ check_approxidate 'noon today' '2009-08-30 12:00:00'
 check_approxidate 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
-check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'

base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0
prerequisite-patch-id: 699b43ba1ff3235d54fa068f4c9b5ab5aa58eacc
prerequisite-patch-id: 2ab5ce03bce8b2953b418fe884f45fbd4a629337
prerequisite-patch-id: 52e3b766b9c6f148f09682b898bf5244b22ae0ba
prerequisite-patch-id: 1cd0e7498368f7c3b1833f12d56fefe732dd0413

Signature · 1 line
2.30.2
#29 lore raw

v5 cover approxidate: tweak special date formats

In reply to Tuomas Ahola
Quoted 2 lines

"Friday noon" asked in the morning on Sunday was parsed to be one day before the specified time, which has been corrected.

Should we add something about the new semantics of "today"?

Tuomas Ahola (4):
approxidate: make "today" wrap to midnight
t0006: add support for approxidate test date adjustment
approxidate: make "specials" respect fixed day-of-month
approxidate: use deferred mday adjustments for "specials"

 Documentation/rev-list-options.adoc |  3 +-
 date.c                              | 46 ++++++++++++++++++++++-------
 t/t0006-date.sh                     | 44 ++++++++++++++++++++++++++-
 3 files changed, 81 insertions(+), 12 deletions(-)

Interdiff mot v4:

date.c +2 -1
index 6e7cf907da..05b78d852f 100644
@@ -1206,12 +1206,13 @@ static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
 	*num = 0;
 }
 
-static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
+static void date_today(struct tm *tm, struct tm *now, int *num)
 {
 	if (tm->tm_hour == now->tm_hour &&
 	    tm->tm_min == now->tm_min &&
 	    tm->tm_sec == now->tm_sec)
 		date_time(tm, 0);
+	*num = 0;
 	tm->tm_mday = -1;
 	update_tm(tm, now, 0);
 }
t/t0006-date.sh +2 -1
index b187b1bfc4..9a76b84ed9 100755
@@ -212,13 +212,14 @@ check_approxidate 'noon today' '2009-08-30 12:00:00'
 check_approxidate 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
-check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
+check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'

base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0

Signature · 1 line
2.30.2
#30 lore raw

PATCH v5 1/4 approxidate: make "today" wrap to midnight

Although some commands do reject invalid approxidate expressions, in other cases those are simply evaluated as the current time. Oftentimes that is a perfectly good compromise to handle silly requests, but it isn't without rough edges.

Because of the silent acceptance, it is easy to forget that "today" isn't actually a valid approxidate format. That is a bit awkward because while the fallback logic of using the current time does make some sense, there is no deliberative decision behind such behavior of "today". Indeed, whatever (non-)action "today" currently has, is just an accidental side effect.

That means "git log --since=today" is currently unlikely to print anything at all as it tries to list commits dated with *future* timestamps. Arguably it would be more useful to list the commits of the current day---i.e. those made since midnight.

On the other hand, "git log --until=today" doesn't really filter commits at all. Changing the definition of "today" would make it return the commits made before the current day. That isn't without problems though---running "git log --until=today" in the late afternoon could reasonably include the work done earlier that day (as the command currently does do).

Still the utility of no-op "--until=today" is debatable and perhaps outweighed by the pros of having "--since=today" to mean "--since=midnight". The thing is that the approxidate machinery doesn't know about its consumers, so the meaning of "today" has to be the same for "--since" and "--until".

In fact, "git log --until=" is documented as

	`--until=<date>`::
	`--before=<date>`::
		Show commits older than _<date>_,

so excluding commits made today would actually match the documentation more closely.

Moreover, a revision parameter "@{today}" is currently outright rejected. Making "today" a valid approxidate time format could make a natural way to specify the state of the ref at the start of the current day.

Bind "today" to new function date_today() as an approxidate special. Make it return the last midnight if no specific time is given; i.e. retain the old behavior of "noon today" and such.

Document the new behavior of "git log --since=today" in rev-list-options.adoc.

---

Notes:

    v4->v5: Add the usual `*num = 0` dance and a test to clarify why it's useful.
 Documentation/rev-list-options.adoc |  3 ++-
 date.c                              | 11 +++++++++++
 t/t0006-date.sh                     |  3 +++
 3 files changed, 16 insertions(+), 1 deletion(-)
Documentation/rev-list-options.adoc +2 -1
index 2d195a1474..a5abadf689 100644
@@ -23,7 +23,8 @@ ordering and formatting options, such as `--reverse`.
 
 `--since=<date>`::
 `--after=<date>`::
-	Show commits more recent than _<date>_.
+	Show commits more recent than _<date>_.  As a special case,
+	'today' means the last midnight.
 
 `--since-as-filter=<date>`::
 	Show all commits more recent than _<date>_. This visits
date.c +11 -0
index 17a95077cf..633d1176fe 100644
@@ -1192,6 +1192,16 @@ static void date_never(struct tm *tm, struct tm *now UNUSED, int *num)
 	*num = 0;
 }
 
+static void date_today(struct tm *tm, struct tm *now, int *num)
+{
+	if (tm->tm_hour == now->tm_hour &&
+	    tm->tm_min == now->tm_min &&
+	    tm->tm_sec == now->tm_sec)
+		date_time(tm, now, 0);
+	*num = 0;
+	update_tm(tm, now, 0);
+}
+
 static const struct special {
 	const char *name;
 	void (*fn)(struct tm *, struct tm *, int *);
@@ -1204,6 +1214,7 @@ static const struct special {
 	{ "AM", date_am },
 	{ "never", date_never },
 	{ "now", date_now },
+	{ "today", date_today },
 	{ NULL }
 };
 
t/t0006-date.sh +3 -0
index 53ced36df4..d95afdda33 100755
@@ -164,6 +164,7 @@ check_approxidate() {
 }
 
 check_approxidate now '2009-08-30 19:20:00'
+check_approxidate today '2009-08-30 00:00:00'
 check_approxidate '5 seconds ago' '2009-08-30 19:19:55'
 check_approxidate 5.seconds.ago '2009-08-30 19:19:55'
 check_approxidate 10.minutes.ago '2009-08-30 19:10:00'
@@ -181,12 +182,14 @@ 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'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
 check_approxidate '06/05/2009' '2009-06-05 19:20:00'
 check_approxidate '06.05.2009' '2009-05-06 19:20:00'
+check_approxidate 'Jan 5 today' '2009-01-30 00:00:00'
 
 check_approxidate 'Jun 6, 5AM' '2009-06-06 05:00:00'
 check_approxidate '5AM Jun 6' '2009-06-06 05:00:00'
Signature · 1 line
2.30.2
#31 lore raw

PATCH v5 2/4 t0006: add support for approxidate test date adjustment

In reply to Tuomas Ahola

t0006 uses a hard-coded test date and provides no convenient way to override it temporarily. Add an optional parameter to check_approxidate to adjust the time as needed, and demonstrate the feature with a new test.

---
 t/t0006-date.sh | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
t/t0006-date.sh +32 -1
index d95afdda33..b9bb7a05d9 100755
@@ -155,12 +155,41 @@ check_parse '2100-00-00 00:00:00 -11' bad
 check_parse '2100-00-00 00:00:00 +11' bad
 REQUIRE_64BIT_TIME=
 
+add_time_offset() {
+	case "$3" in
+	hours)
+		unit=$(( 60*60 ))
+		;;
+	days)
+		unit=$(( 24*60*60 ))
+		;;
+	esac
+	offset=$(( $2 * unit ))
+	echo $(( $1 + offset ))
+}
+
 check_approxidate() {
+	old_date=$GIT_TEST_DATE_NOW
+	if test "$3" = "failure"
+	then
+		expection="$3"
+	else
+		expection=${4:-success}
+		offset="$3"
+	fi
+	if test -n "$offset"
+	then
+		GIT_TEST_DATE_NOW=$(add_time_offset $old_date $offset)
+		caption="$1; offset $offset"
+	else
+		caption=$1
+	fi
 	echo "$1 -> $2 +0000" >expect
-	test_expect_${3:-success} "parse approxidate ($1)" "
+	test_expect_$expection "parse approxidate ($caption)" "
 	test-tool date approxidate '$1' >actual &&
 	test_cmp expect actual
 	"
+	GIT_TEST_DATE_NOW=$old_date
 }
 
 check_approxidate now '2009-08-30 19:20:00'
@@ -184,6 +213,8 @@ check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
+check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
+check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'
 
 check_approxidate 'last tuesday' '2009-08-25 19:20:00'
 check_approxidate 'July 5th' '2009-07-05 19:20:00'
Signature · 1 line
2.30.2
#32 lore raw

PATCH v5 3/4 approxidate: make "specials" respect fixed day-of-month

In reply to Tuomas Ahola

The special approxidate time formats, "noon" and "tea" differ from "12pm" and "5pm" by having the feature of wrapping to the previous day if the current time is before those hours:

	now  -> 2026-05-13 11:00:00 +0000

	12pm -> 2026-05-13 12:00:00 +0000
	5pm  -> 2026-05-13 17:00:00 +0000

	noon -> 2026-05-12 12:00:00 +0000
	tea  -> 2026-05-12 17:00:00 +0000

However, that logic carries too far. Even when the date is specified, the behavior of the "specials" depends on the current time. Assuming the same time as above, we get:

	today at noon -> 2026-05-12 12:00:00 +0000 (should be 13 May)
	13 May at tea -> 2026-05-12 17:00:00 +0000

or, using an example mentioned in date-formats.adoc:

	last Friday at noon -> 2026-05-07 12:00:00 +0000 (should be 8 May)

The quirk seems to be rather old. Already in 2006, Linus Torvalds remarked that the date yielded by "one year ago yesterday at tea-time" was "just silly and not even correct". Indeed, even today it gives:

	One year ago yesterday at tea-time -> 2025-05-11 17:00:00 +0000
	  (should be 12 May)

Let's fix all of those with a simple patch. Check whether we already have a specified day-of-month in tm->tm_mday and make date_time() stick to it. Ensure the correct behavior with relevant tests.

Links:
1. https://lore.kernel.org/git/[email protected]/

---
 date.c          | 6 +++++-
 t/t0006-date.sh | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)
date.c +5 -1
index 633d1176fe..1e9cfe4b6f 100644
@@ -1132,7 +1132,11 @@ static void date_yesterday(struct tm *tm, struct tm *now, int *num)
 
 static void date_time(struct tm *tm, struct tm *now, int hour)
 {
-	if (tm->tm_hour < hour)
+	/*
+	 * If we do not yet have a specified day, we'll use the most recent
+	 * version of "hour" relative to now.  But that may be yesterday.
+	 */
+	if (tm->tm_mday < 0 && tm->tm_hour < hour)
 		update_tm(tm, now, 24*60*60);
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
t/t0006-date.sh +4 -0
index b9bb7a05d9..62cbada774 100755
@@ -209,8 +209,12 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
+check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
Signature · 1 line
2.30.2
#33 lore raw

PATCH v5 4/4 approxidate: use deferred mday adjustments for "specials"

In reply to Tuomas Ahola

There are cases where the "wrap-to-yesterday" behavior of "tea" and "noon" should be reverted later on down the line, so that "today tea" and "tea today" won't yield different results. However, the logic of approxidate doesn't seem to lend itself particularly well to such cases.

Start tackling the issue by reusing negative values of tm->tm_mday field for deferred date adjustments which can be easily reverted, so that the default logic of the special formats only applies if we don't get any explicit date (mday) specification. In particular, overwrite the field with -1 in "today" and "yesterday", so that those formats will be relative to the current date. That makes specifications like "tea yesterday" behave more sensibly: instead of going backwards to the last tea-time and then a day back, Git will now understand that as the tea-time of yesterday.

Replace the call of update_tm() in date_time() with the assignment tm->tm_mday = -2. Add the corresponding code to handle that in update_tm(), wrapping to the previous day if the field still holds such assignment, meaning that we haven't seen any better specification for the day-of-month. On the other hand, mday=-3 would mean going two days back and so on. Even though such functionality isn't actually needed by this patch, it won't add much complexity in the code and is rather natural way to handle such values.

As date_time() won't no longer need the now struct, mark the associated function parameters as unused. The parameters themselves have to stay, however, as those functions are called through pointers in approxidate_alpha. Add relevant tests to cover the changes.

---

Notes:

    v4->v5: apply the earlier fixup patch
 date.c          | 31 +++++++++++++++++++++----------
 t/t0006-date.sh |  4 ++++
 2 files changed, 25 insertions(+), 10 deletions(-)
date.c +21 -10
index 1e9cfe4b6f..05b78d852f 100644
@@ -1071,13 +1071,22 @@ void datestamp(struct strbuf *out)
 /*
  * Relative time update (eg "2 days ago").  If we haven't set the time
  * yet, we need to set it from current time.
+ *
+ * The tm->tm_mday field has an additional logic of using negative values
+ * for date adjustments: -2 means yesterday and -3 the day before that,
+ * and so on.  The idea is to deref such adjustments until we are sure
+ * there's no explicit mday specification in the approxidate string.
  */
 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
 {
 	time_t n;
 
-	if (tm->tm_mday < 0)
+	if (tm->tm_mday < 0) {
+		int offset = tm->tm_mday + 1;
+		if (sec == 0 && offset < 0)
+			sec = -offset * 24*60*60;
 		tm->tm_mday = now->tm_mday;
+	}
 	if (tm->tm_mon < 0)
 		tm->tm_mon = now->tm_mon;
 	if (tm->tm_year < 0) {
@@ -1127,38 +1136,39 @@ static void date_now(struct tm *tm, struct tm *now, int *num)
 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);
 }
 
-static void date_time(struct tm *tm, struct tm *now, int hour)
+static void date_time(struct tm *tm, int hour)
 {
 	/*
 	 * If we do not yet have a specified day, we'll use the most recent
 	 * version of "hour" relative to now.  But that may be yesterday.
 	 */
 	if (tm->tm_mday < 0 && tm->tm_hour < hour)
-		update_tm(tm, now, 24*60*60);
+		tm->tm_mday = -2; /* eventually handled by update_tm() */
 	tm->tm_hour = hour;
 	tm->tm_min = 0;
 	tm->tm_sec = 0;
 }
 
-static void date_midnight(struct tm *tm, struct tm *now, int *num)
+static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 0);
+	date_time(tm, 0);
 }
 
-static void date_noon(struct tm *tm, struct tm *now, int *num)
+static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 12);
+	date_time(tm, 12);
 }
 
-static void date_tea(struct tm *tm, struct tm *now, int *num)
+static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
 {
 	pending_number(tm, num);
-	date_time(tm, now, 17);
+	date_time(tm, 17);
 }
 
 static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
@@ -1201,8 +1211,9 @@ static void date_today(struct tm *tm, struct tm *now, int *num)
 	if (tm->tm_hour == now->tm_hour &&
 	    tm->tm_min == now->tm_min &&
 	    tm->tm_sec == now->tm_sec)
-		date_time(tm, now, 0);
+		date_time(tm, 0);
 	*num = 0;
+	tm->tm_mday = -1;
 	update_tm(tm, now, 0);
 }
 
t/t0006-date.sh +4 -0
index 62cbada774..9a76b84ed9 100755
@@ -210,9 +210,13 @@ 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 'today at noon' '2009-08-30 12:00:00' '-12 hours'
+check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
+check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
 check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
Signature · 1 line
2.30.2
#34 lore raw
In reply to Tuomas Ahola

date.c static void date_never(struct tm *tm, struct tm *now UNUSED, int *num) · line 1218

Quoted 11 lines · Tuomas Ahola
date.c +2 -1
-static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
+static void date_today(struct tm *tm, struct tm *now, int *num)
 {
 	if (tm->tm_hour == now->tm_hour &&
 	    tm->tm_min == now->tm_min &&
 	    tm->tm_sec == now->tm_sec)
 		date_time(tm, 0);
+	*num = 0;
 	tm->tm_mday = -1;
 	update_tm(tm, now, 0);
 }

Hmph, what is this change about? Does the lack of this clearing break some test?

In any case, will queue. It seems that we are getting to the point of diminishing returns and better off declaring victory soonish?

Quoted 22 lines
t/t0006-date.sh +2 -1
index b187b1bfc4..9a76b84ed9 100755
@@ -212,13 +212,14 @@ check_approxidate 'noon today' '2009-08-30 12:00:00'
 check_approxidate 'today at noon' '2009-08-30 12:00:00' '-12 hours'
 check_approxidate 'noon today' '2009-09-01 12:00:00' '+36 hours'
 check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00'
 check_approxidate 'last Friday at noon' '2009-08-28 12:00:00' '-12 hours'
-check_approxidate 'noon yesterday' '2009-08-29 12:00:00' '-12 hours'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00'
 check_approxidate 'tea last saturday' '2009-08-29 17:00:00' '-12 hours'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
 check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' '-12 hours'
+check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'
 check_approxidate '10am noon' '2009-08-29 12:00:00'
 check_approxidate 'January 5th yesterday' '2009-01-29 19:20:00'
 check_approxidate 'January 5th yesterday' '2008-12-31 19:20:00' '+2 days'

base-commit: 94f057755b7941b321fd11fec1b2e3ca5313a4e0

#35 lore raw
Quoted 17 lines · Junio C Hamano

date.c static void date_never(struct tm *tm, struct tm *now UNUSED, int *num) · line 1218

Quoted 11 lines · Tuomas Ahola
date.c +2 -1
-static void date_today(struct tm *tm, struct tm *now, int *num UNUSED)
+static void date_today(struct tm *tm, struct tm *now, int *num)
 {
 	if (tm->tm_hour == now->tm_hour &&
 	    tm->tm_min == now->tm_min &&
 	    tm->tm_sec == now->tm_sec)
 		date_time(tm, 0);
+	*num = 0;
 	tm->tm_mday = -1;
 	update_tm(tm, now, 0);
 }

Hmph, what is this change about? Does the lack of this clearing break some test?

As you can see, many other date_*() functions have that same assignment, too. It looked a bit off, so I first left it out, but this revision does add a corner case test[*] that would fail without it.

Quoted 3 lines

In any case, will queue. It seems that we are getting to the point of diminishing returns and better off declaring victory soonish?

Yes, I agree. The patch series is effectively finished as I don't have any further itches or ideas. And I think the series gets its work done quite well actually.

Quoted 1 line
Quoted 1 line

+check_approxidate 'January 5th today pm' '2009-01-30 12:00:00'

[*] Namely, this one.