How can I ask for the date format string that matches the user's current Gnome locale setting

Hi, everyone.

I am trying to figure out how to ask Gnome for the correct string that is used for formatting a date according to the user’s settings. I’m working on an Electron application, so I don’t get to just use Gnome widgets, but I am able to call C APIs to get the relevant information.

So, as part of my research, I set an FC39, Gnome 45 system to still use English as my language, but Russian as my date format. I wrote an application which queries relevant values from get_langinfo. And then I took a screenshot of a bunch of these things all side-by-side:

0d4b24b3-9a59-45cd-a301-bbe818fa2681 (PNG Image, 1419 × 630 pixels)

The gist of the screenshot is this. According to my application, get_langinfo(D_FMT) returns “%m/%d/%y”. At the same time, the Gnome Settings Region Format panel shows that today’s date should be rendered as 17.11.2023, which is the %d.%m.%y format. Further, Gnome is showing the current day, month in Russion, but it appears that it’s the same format as a US date.

So, the crux of the question.

I’m wanting to display a date, or display a date entry field, in the correct format that the user has set on their system. How do I ask the system what that format should be, without delegating the formatting to Gnome widgets?

I searched the source code of your screenshot, i.e. grepped “Date & Times” in gnome-control-center source code, and you can see the code being used, which is using g_date_time_format() and functions from locale.h so from a glimpse I think it’s doing as follows:

// Creating a new locale with a specific region
locale = newlocale (LC_TIME_MASK, region, (locale_t) 0);
// Setting that locale as the currently active, while saving the previous one
old_locale = uselocale (locale);
// Use g_date_time_format() to extract formats of current locale
GDateTime *dt = g_date_time_new_now_local ();
gchar *date_format = g_date_time_format (dt, "%c");
// Cleanup: Restablish old_locale, free the locale we created, free date_time object
if (locale != (locale_t) 0) {
  uselocale (old_locale);
  freelocale (locale);
g_date_time_unref (dt);

I’m sorry, it’s been a very busy last few weeks. I will look into this. I had never seen g_date_time_format and will have to plug it into the app that I use for testing this.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.