From 11294bd1ac784d875962818d2727998ab8282c74 Mon Sep 17 00:00:00 2001 From: Hari Rana Date: Sun, 20 Sep 2026 13:28:04 -0400 Subject: [PATCH] utils: Add macro for computing days after first weekday Currently, `gcal_date_time_get_start_of_week()` seems to be the only user of this macro function. In the next commit, the month view will use it to traverse weeks while maintaining the weekday. Part-of: --- src/utils/gcal-date-time-utils.c | 2 +- src/utils/gcal-utils.h | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/gcal-date-time-utils.c b/src/utils/gcal-date-time-utils.c index 84b8728a..ed1b8455 100644 --- a/src/utils/gcal-date-time-utils.c +++ b/src/utils/gcal-date-time-utils.c @@ -90,7 +90,7 @@ gcal_date_time_get_start_of_week (GDateTime *date) first_weekday = gcal_context_get_week_start_day (context); weekday = g_date_time_get_day_of_week (date) % GCAL_N_WEEKDAYS; - n_days_after_week_start = (GCAL_N_WEEKDAYS + weekday - first_weekday) % GCAL_N_WEEKDAYS; + n_days_after_week_start = GCAL_NTH_WEEKDAY (weekday, first_weekday); start_of_week = g_date_time_add_days (date, -n_days_after_week_start); diff --git a/src/utils/gcal-utils.h b/src/utils/gcal-utils.h index 4b8702f2..ef685f23 100644 --- a/src/utils/gcal-utils.h +++ b/src/utils/gcal-utils.h @@ -32,6 +32,15 @@ #define GCAL_MINUTES_PER_DAY 1440 #define GCAL_MAX_MINUTES (GCAL_N_WEEKDAYS * GCAL_MINUTES_PER_DAY) +/** + * GCAL_NTH_WEEKDAY: + * + * Computes days after the first weekday for use in conjunction + * with [struct@GLib.DateTime]. + */ +#define GCAL_NTH_WEEKDAY(weekday, first_weekday) \ + (weekday - first_weekday + GCAL_N_WEEKDAYS) % GCAL_N_WEEKDAYS + #define GCAL_DEFAULT_APPLICATION GCAL_APPLICATION (g_application_get_default ()) #define gcal_clear_timeout(pp) { if (pp && *pp) { g_source_remove (*pp); *pp = 0; } }