From a237eacfe5ad5fdd7014deca220e8f9ea9f4bda3 Mon Sep 17 00:00:00 2001 From: Tuomas Ahola Date: Thu, 21 May 2026 13:54:05 +0300 Subject: [PATCH 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=`:: `--before=`:: Show commits older than __, 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. Signed-off-by: Tuomas Ahola Signed-off-by: Junio C Hamano --- Documentation/rev-list-options.adoc | 3 ++- date.c | 11 +++++++++++ t/t0006-date.sh | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc index 2d195a1474..a5abadf689 100644 --- a/Documentation/rev-list-options.adoc +++ b/Documentation/rev-list-options.adoc @@ -23,7 +23,8 @@ ordering and formatting options, such as `--reverse`. `--since=`:: `--after=`:: - Show commits more recent than __. + Show commits more recent than __. As a special case, + 'today' means the last midnight. `--since-as-filter=`:: Show all commits more recent than __. This visits diff --git a/date.c b/date.c index 17a95077cf..633d1176fe 100644 --- a/date.c +++ b/date.c @@ -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 } }; diff --git a/t/t0006-date.sh b/t/t0006-date.sh index 53ced36df4..d95afdda33 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -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' From d5ba64d8b5bb76402a64af2b780780de952de2b9 Mon Sep 17 00:00:00 2001 From: Tuomas Ahola Date: Thu, 21 May 2026 13:54:06 +0300 Subject: [PATCH 2/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. Signed-off-by: Tuomas Ahola Signed-off-by: Junio C Hamano --- t/t0006-date.sh | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/t/t0006-date.sh b/t/t0006-date.sh index d95afdda33..b9bb7a05d9 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -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' From bbe9b46ac826b5dad4f714513638dd223b6fe63f Mon Sep 17 00:00:00 2001 From: Tuomas Ahola Date: Thu, 21 May 2026 13:54:07 +0300 Subject: [PATCH 3/4] approxidate: make "specials" respect fixed day-of-month 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/Pine.LNX.4.64.0610101102560.3952@g5.osdl.org/ Signed-off-by: Tuomas Ahola Signed-off-by: Junio C Hamano --- date.c | 6 +++++- t/t0006-date.sh | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/date.c b/date.c index 633d1176fe..1e9cfe4b6f 100644 --- a/date.c +++ b/date.c @@ -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; diff --git a/t/t0006-date.sh b/t/t0006-date.sh index b9bb7a05d9..62cbada774 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -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' From b809304101635d0fafc7644e82704ae653cacb07 Mon Sep 17 00:00:00 2001 From: Tuomas Ahola Date: Thu, 21 May 2026 13:54:08 +0300 Subject: [PATCH 4/4] approxidate: use deferred mday adjustments for "specials" 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. Signed-off-by: Tuomas Ahola Signed-off-by: Junio C Hamano --- date.c | 31 +++++++++++++++++++++---------- t/t0006-date.sh | 4 ++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/date.c b/date.c index 1e9cfe4b6f..05b78d852f 100644 --- a/date.c +++ b/date.c @@ -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); } diff --git a/t/t0006-date.sh b/t/t0006-date.sh index 62cbada774..9a76b84ed9 100755 --- a/t/t0006-date.sh +++ b/t/t0006-date.sh @@ -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'