From: Michael Tremer Date: Fri, 27 Mar 2026 12:30:01 +0000 (+0000) Subject: graph: Fetch temperature unit from locale X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab232b60272cfaad8faa564c597f189f43d15234;p=telemetry.git graph: Fetch temperature unit from locale Signed-off-by: Michael Tremer --- diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 5319d93..6f98324 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -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,