#############################################################################*/
#include <errno.h>
+#include <langinfo.h>
#include <limits.h>
#include <locale.h>
#include <stdlib.h>
}
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,