From c5548f08e4244994085232917ebc3a7d7c60366e Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 16 May 2025 18:20:40 +1000 Subject: [PATCH] Fix display of relative times Change "min." to "minutes" and fixes nonsense times such as "2020-01-01 ago" --- themed/commit.html | 2 +- themed/log.html | 2 +- themed/refs.html | 6 +++--- themed/summary.html | 2 +- ui-shared.c | 43 +++++++++++++++++++++++++++++++++++++++++++ ui-shared.h | 1 + 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/themed/commit.html b/themed/commit.html index 9f5ec47..b7f9f59 100644 --- a/themed/commit.html +++ b/themed/commit.html @@ -36,7 +36,7 @@
{{ info->author }} - {! cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2); !} ago + {! cgit_print_age_themed(info->committer_date, info->committer_tz, TM_MONTH * 12); !}
{% for struct commit_list *p = commit->parents; p; p = p->next %} diff --git a/themed/log.html b/themed/log.html index d47436b..850e11d 100644 --- a/themed/log.html +++ b/themed/log.html @@ -32,7 +32,7 @@
object.oid), ctx.qry.vpath); !}" class="hover:text-blue-600 hover:underline">{{ info->subject }}
- {{ info->author }} committed {! cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2); !} ago + {{ info->author }} committed {! cgit_print_age_themed(info->committer_date, info->committer_tz, TM_MONTH * 12); !}
diff --git a/themed/refs.html b/themed/refs.html index 80b4e52..18d7d2d 100644 --- a/themed/refs.html +++ b/themed/refs.html @@ -35,7 +35,7 @@
{{ name }}
{% if ref->object->type == OBJ_COMMIT %}
- {{ info->subject }} · Updated {! cgit_print_age(info->committer_date, info->committer_tz, -1); !} ago + {{ info->subject }} · Updated {! cgit_print_age_themed(info->committer_date, info->committer_tz, TM_MONTH * 12); !} {{ info->author }}
@@ -86,7 +86,7 @@ {% if info && (info->tagger_date > 0 || info->tagger) %}
{% if info->tagger_date > 0 %} - Updated {! cgit_print_age(info->tagger_date, info->tagger_tz, -1); !} ago + Updated {! cgit_print_age_themed(info->tagger_date, info->tagger_tz, TM_MONTH * 12); !} {% endif %} {% if info->tagger %} @@ -95,7 +95,7 @@
{% elif ref->object->type == OBJ_COMMIT %}
- Updated {! cgit_print_age(ref->commit->commit->date, 0, -1); !} ago + Updated {! cgit_print_age_themed(ref->commit->commit->date, 0, TM_MONTH * 12); !} {{ ref->commit->author }}
diff --git a/themed/summary.html b/themed/summary.html index bcf855c..be88b54 100644 --- a/themed/summary.html +++ b/themed/summary.html @@ -39,7 +39,7 @@ {! short_commit_id(oid_to_hex(&commit->object.oid)); !} · - {! cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2); !} ago + {! cgit_print_age_themed(info->committer_date, info->committer_tz, TM_MONTH * 12); !}
{! cgit_free_commitinfo(info); !} diff --git a/ui-shared.c b/ui-shared.c index 92d399f..d28d903 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -784,6 +784,49 @@ void cgit_print_age(time_t t, int tz, time_t max_relative) print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years"); } +void cgit_print_age_themed(time_t t, int tz, time_t max_relative) +{ + time_t now, secs; + + if (!t) + return; + time(&now); + secs = now - t; + if (secs < 0) + secs = 0; + + if (secs > max_relative && max_relative >= 0) { + html(""); + html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT))); + html(""); + return; + } + + if (secs < TM_HOUR * 2) { + print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "minutes ago"); + return; + } + if (secs < TM_DAY * 2) { + print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours ago"); + return; + } + if (secs < TM_WEEK * 2) { + print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days ago"); + return; + } + if (secs < TM_MONTH * 2) { + print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks ago"); + return; + } + if (secs < TM_YEAR * 2) { + print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months ago"); + return; + } + print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years ago"); +} + void cgit_print_http_headers(void) { if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1")) diff --git a/ui-shared.h b/ui-shared.h index 5152030..8e3833e 100644 --- a/ui-shared.h +++ b/ui-shared.h @@ -73,6 +73,7 @@ __attribute__((format (printf,1,0))) extern void cgit_vprint_error(const char *fmt, va_list ap); extern const struct date_mode cgit_date_mode(enum date_mode_type type); extern void cgit_print_age(time_t t, int tz, time_t max_relative); +extern void cgit_print_age_themed(time_t t, int tz, time_t max_relative); extern void cgit_print_http_headers(void); extern void cgit_redirect(const char *url, bool permanent); extern void cgit_print_docstart(void);