]> git.ipfire.org Git - thirdparty/git.git/commitdiff
approxidate: make "today" wrap to midnight
authorTuomas Ahola <taahol@utu.fi>
Thu, 21 May 2026 10:54:05 +0000 (13:54 +0300)
committerJunio C Hamano <gitster@pobox.com>
Thu, 21 May 2026 13:30:05 +0000 (22:30 +0900)
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.

Signed-off-by: Tuomas Ahola <taahol@utu.fi>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/rev-list-options.adoc
date.c
t/t0006-date.sh

index 2d195a147456ead9f517cea18d1bfbe86c941e76..a5abadf689cac855e5e9b0e775dbf36050f36d4c 100644 (file)
@@ -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
diff --git a/date.c b/date.c
index 17a95077cf5450206d9acdbfcefc70a2656072ee..633d1176fe90b929c342e446fdad99b9c70b58bb 100644 (file)
--- 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 }
 };
 
index 53ced36df448f1b572eb39fd76b8de291c529d69..d95afdda33ffd27a7fa6e75ca75cf75fd1417d6a 100755 (executable)
@@ -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'