From b8c46f4565c3fc1babc01f6792ea9850feebf3f2 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 13:22:57 -0700 Subject: Step a stats period with one function --- source/ui-stats.c | 65 +++++++++++++++---------------------------------------- 1 file changed, 18 insertions(+), 47 deletions(-) (limited to 'source/ui-stats.c') diff --git a/source/ui-stats.c b/source/ui-stats.c index 9c79e74..6b05c80 100644 --- a/source/ui-stats.c +++ b/source/ui-stats.c @@ -35,19 +35,11 @@ static void trunc_week(struct tm *tm) gmtime_r(&t, tm); } -static void dec_week(struct tm *tm) +static void step_week(struct tm *tm, int n) { time_t t = timegm(tm); - t -= SECONDS_PER_WEEK; - gmtime_r(&t, tm); -} - -static void inc_week(struct tm *tm) -{ - time_t t = timegm(tm); - - t += SECONDS_PER_WEEK; + t += n * SECONDS_PER_WEEK; gmtime_r(&t, tm); } @@ -68,21 +60,15 @@ static void trunc_month(struct tm *tm) tm->tm_mday = 1; } -static void dec_month(struct tm *tm) +static void step_month(struct tm *tm, int n) { - tm->tm_mon--; + tm->tm_mon += n; if (tm->tm_mon < 0) { tm->tm_year--; - tm->tm_mon = 11; - } -} - -static void inc_month(struct tm *tm) -{ - tm->tm_mon++; - if (tm->tm_mon > 11) { + tm->tm_mon += 12; + } else if (tm->tm_mon > 11) { tm->tm_year++; - tm->tm_mon = 0; + tm->tm_mon -= 12; } } @@ -98,22 +84,12 @@ static char *pretty_month(struct tm *tm) static void trunc_quarter(struct tm *tm) { trunc_month(tm); - while (tm->tm_mon % 3 != 0) - dec_month(tm); -} - -static void dec_quarter(struct tm *tm) -{ - dec_month(tm); - dec_month(tm); - dec_month(tm); + tm->tm_mon -= tm->tm_mon % 3; } -static void inc_quarter(struct tm *tm) +static void step_quarter(struct tm *tm, int n) { - inc_month(tm); - inc_month(tm); - inc_month(tm); + step_month(tm, 3 * n); } static char *pretty_quarter(struct tm *tm) @@ -127,14 +103,9 @@ static void trunc_year(struct tm *tm) tm->tm_mon = 0; } -static void dec_year(struct tm *tm) -{ - tm->tm_year--; -} - -static void inc_year(struct tm *tm) +static void step_year(struct tm *tm, int n) { - tm->tm_year++; + tm->tm_year += n; } static char *pretty_year(struct tm *tm) @@ -145,10 +116,10 @@ static char *pretty_year(struct tm *tm) // The order runs from the finest window to the coarsest, because a repository // caps the page by storing an index into this table as its max-stats. static const struct cgit_period periods[] = { - {'w', "week", 4, trunc_week, dec_week, inc_week, pretty_week}, - {'m', "month", 4, trunc_month, dec_month, inc_month, pretty_month}, - {'q', "quarter", 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter}, - {'y', "year", 4, trunc_year, dec_year, inc_year, pretty_year}, + {'w', "week", 4, trunc_week, step_week, pretty_week}, + {'m', "month", 4, trunc_month, step_month, pretty_month}, + {'q', "quarter", 4, trunc_quarter, step_quarter, pretty_quarter}, + {'y', "year", 4, trunc_year, step_year, pretty_year}, }; static void window_start(const struct cgit_period *period, struct tm *tm) @@ -167,7 +138,7 @@ static void window_start(const struct cgit_period *period, struct tm *tm) tm->tm_min = 0; tm->tm_sec = 0; for (i = 1; i < period->count; i++) - period->dec(tm); + period->step(tm, -1); } static void add_commit(struct string_list *authors, struct commitinfo *info, @@ -283,7 +254,7 @@ static struct string_list build_period_labels(const struct cgit_period *period) window_start(period, &tm); for (i = 0; i < period->count; i++) { string_list_append(&labels, period->pretty(&tm)); - period->inc(&tm); + period->step(&tm, 1); } return labels; } -- cgit 2.11.1