diff --git a/server/scripts/modules/regionalforecast-utils.mjs b/server/scripts/modules/regionalforecast-utils.mjs index 02e434d..5672396 100644 --- a/server/scripts/modules/regionalforecast-utils.mjs +++ b/server/scripts/modules/regionalforecast-utils.mjs @@ -1,16 +1,21 @@ import { getWeatherRegionalIconFromIconLink } from './icons.mjs'; import { preloadImg } from './utils/image.mjs'; import { json } from './utils/fetch.mjs'; +import { temperature as temperatureUnit } from './utils/units.mjs'; -const buildForecast = (forecast, city, cityXY) => ({ - daytime: forecast.isDaytime, - temperature: forecast.temperature || 0, - name: formatCity(city.city), - icon: forecast.icon, - x: cityXY.x, - y: cityXY.y, - time: forecast.startTime, -}); +const buildForecast = (forecast, city, cityXY) => { + // get a unit converter + const temperatureConverter = temperatureUnit('us'); + return { + daytime: forecast.isDaytime, + temperature: temperatureConverter(forecast.temperature || 0), + name: formatCity(city.city), + icon: forecast.icon, + x: cityXY.x, + y: cityXY.y, + time: forecast.startTime, + }; +}; const getRegionalObservation = async (point, city) => { try { diff --git a/server/scripts/modules/utils/units.mjs b/server/scripts/modules/utils/units.mjs index e612fe2..d829592 100644 --- a/server/scripts/modules/utils/units.mjs +++ b/server/scripts/modules/utils/units.mjs @@ -7,6 +7,7 @@ const round2 = (value, decimals) => Math.trunc(value * 10 ** decimals) / 10 ** d const kphToMph = (Kph) => Math.round(Kph / 1.609_34); const celsiusToFahrenheit = (Celsius) => Math.round((Celsius * 9) / 5 + 32); +const fahrenheitToCelsius = (Fahrenheit) => Math.round((Fahrenheit - 32) * 5 / 9); const kilometersToMiles = (Kilometers) => Math.round(Kilometers / 1.609_34); const metersToFeet = (Meters) => Math.round(Meters / 0.3048); const pascalToInHg = (Pascal) => round2(Pascal * 0.000_295_3, 2); @@ -35,7 +36,11 @@ const temperature = (defaultUnit = 'si') => { let converter = (passthru) => Math.round(passthru); // change the converter if there is a mismatch if (defaultUnit !== settings.units.value) { - converter = celsiusToFahrenheit; + if (defaultUnit === 'us') { + converter = fahrenheitToCelsius; + } else { + converter = celsiusToFahrenheit; + } } // append units if (settings.units.value === 'si') {