]> git.ipfire.org Git - telemetry.git/commitdiff
graph: Fetch temperature unit from locale
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Mar 2026 12:30:01 +0000 (12:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Mar 2026 12:30:01 +0000 (12:30 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/graph.c

index 5319d93f58eb426f2b19e2269049ee9525dbd333..6f98324e7e06addad1301e221978615540c2b3d9 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <langinfo.h>
 #include <limits.h>
 #include <locale.h>
 #include <stdlib.h>
@@ -522,39 +523,44 @@ ERROR:
 }
 
 td_graph_temps td_graph_temperature(const td_graph_render_options* options) {
-       // These countries predominantly use Fahrenheit
-       const char* fahrenheit[] = {
-               // United States
-               "en_US",
-               // Bahamas
-               "en_BS",
-               // Belize
-               "en_BZ",
-               // Cayman Islands
-               "en_KY",
-               // Liberia
-               "en_LR",
-               // Palau
-               "en_PW",
-               // Marshall Islands
-               "en_MH",
-               // Micronesia
-               "en_FM",
-               NULL,
-       };
+       locale_t loc = NULL;
 
-       // If there is no locale, we will use Kelvin
-       if (!options->locale || !*options->locale || td_string_equals(options->locale, "C"))
-               return TD_GRAPH_TEMP_KELVIN;
+       // Use Kelvin by default
+       td_graph_temps t = TD_GRAPH_TEMP_KELVIN;
 
-       // Find anyone using Fahrenheit
-       for (const char** code = fahrenheit; *code; code++) {
-               if (td_string_startswith(options->locale, *code))
-                       return TD_GRAPH_TEMP_FAHRENHEIT;
+       // If there is no locale, we will use Kelvin
+       if (!options->locale || !*options->locale)
+               goto DONE;
+
+       // Load the given locale
+       loc = newlocale(LC_MEASUREMENT_MASK, options->locale, (locale_t)0);
+       if (!loc)
+               goto DONE;
+
+       uint8_t measurement = (uint8_t)*nl_langinfo_l(_NL_MEASUREMENT_MEASUREMENT, loc);
+
+       // Select temperature measurement
+       switch (measurement) {
+               // Metric
+               case 1:
+                       t = TD_GRAPH_TEMP_CELSIUS;
+                       break;
+
+               // Imperial
+               case 2:
+                       t = TD_GRAPH_TEMP_FAHRENHEIT;
+                       break;
+
+               // Ignore anything else and stick with the default
+               default:
+                       break;
        }
 
-       // Otherwise use Celsius
-       return TD_GRAPH_TEMP_CELSIUS;
+DONE:
+       if (loc)
+               freelocale(loc);
+
+       return t;
 }
 
 int td_graph_vlabel_bps(td_ctx* ctx, td_graph* graph,