utils: Use ISO weekday numbering for 'get_first_weekday' function

Change the 'get_first_weekday' function to number the weekdays
Monday(1)-Sunday(7) like ISO does. Change the function name to reflect
that. Add tests for different locales, although the tests depend on
system-installed locales.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-calendar/-/merge_requests/822>
This commit is contained in:
mindonwarp
2026-09-26 17:22:00 +00:00
committed by Marge Bot
parent a0a6ae2a2c
commit acd8fbaf69
8 changed files with 74 additions and 25 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ gcal_date_time_get_start_of_week (GDateTime *date)
g_assert (date != NULL);
first_weekday = gcal_util_get_first_weekday ();
first_weekday = gcal_util_get_first_weekday_iso ();
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;
+24 -14
View File
@@ -213,36 +213,46 @@ gcal_util_get_desc_from_component (ECalComponent *component,
/**
* gcal_util_get_first_weekday:
*
* Copied from Clocks, which by itself is
* copied from GtkCalendar.
* Finds the locale default first day of week
* respecting the ISO day counting (Monday: 1 - Sunday: 7).
*
* Returns: the first weekday, from 0 to 6
* Prefer using `GcalContext:week-start-day` property or
* the `GcalContext:gcal_context_get_week_start_day()` method
* to get the current setting from gsettings-desktop-schemas.
* Use this utility method only as a fallback.
*
* Returns: the first weekday, from 1 to 7
*/
gint
gcal_util_get_first_weekday (void)
gcal_util_get_first_weekday_iso (void)
{
gint week_start;
#ifdef HAVE__NL_TIME_FIRST_WEEKDAY
union { unsigned int word; char *string; } langinfo;
gint week_1stday = 0;
union { unsigned int baseline; char *offset; } langinfo;
gint iso_week_base = 7; /* default to Sunday (19971130) */
gint first_weekday = 1;
guint week_origin;
langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
first_weekday = langinfo.string[0];
langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
week_origin = langinfo.word;
/* first day of week index */
langinfo.offset = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
first_weekday = langinfo.offset[0];
/* find the baseline week origin */
langinfo.offset = nl_langinfo (_NL_TIME_WEEK_1STDAY);
week_origin = langinfo.baseline;
if (week_origin == 19971130) /* Sunday */
week_1stday = 0;
iso_week_base = 7;
else if (week_origin == 19971201) /* Monday */
week_1stday = 1;
iso_week_base = 1;
else if (week_origin == 19971129) /* unusual: Saturday */
iso_week_base = 6;
else
g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
week_start = (week_1stday + first_weekday - 1) % GCAL_N_WEEKDAYS;
week_start = ((iso_week_base - 1 + (first_weekday - 1)) % GCAL_N_WEEKDAYS) + 1;
#else
gchar *gtk_week_start;
+1 -1
View File
@@ -63,7 +63,7 @@ GdkPaintable* gcal_util_get_circle_paintable_from_color (const GdkRGBA
gchar* gcal_util_get_desc_from_component (ECalComponent *component,
const gchar *joint_char);
gint gcal_util_get_first_weekday (void);
gint gcal_util_get_first_weekday_iso (void);
ECalComponent* gcal_util_build_component_from_details (const gchar *summary,
GDateTime *initial_date,