You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
async function fetchDayLengthValues(geoid, timezone) {
const now = new Date();
// Substract one hour, because timeseries returns the next even hour with Sunrise and Sunset
// This means that whenever time would be for example 23:01 localtime, it would return
// 00:00 values, which are already the sunrise and sunset values for the next day.
// This way we get the correct values for the current day, because it changes to return the
// previous even hour values.
now.setHours(now.getHours() - 1);
// Use swedish locale with selected place timezone. Sweden uses timeseries compliant ISO-8601 standard
const formattedStartTime = now.toLocaleString('sv-SE', { timeZone: timezone });
const params = {
param: 'Sunrise,Sunset',
starttime: formattedStartTime,
format: 'json',
geoid,
tz: timezone,
timesteps: 1,
who: 'wau2-api',
};
let data = [];
try {
const start = process.hrtime();
const response = await axios.get(getTimeseriesUrl(), {
params,
});
const duration = getDurationInMilliseconds(start);
if (process.env.ENV !== 'prod') {
console.log(`fetchDayLengthValues [FINISHED] ${duration.toLocaleString()} ms`);
}
data = response.data;
} catch (e) {
logException(e, 'Error in loading day length values');
// don't send error code
return false;
}
return data;
}
if (!data.Sunrise || !data.Sunset) {
return {};
}
let sunriseAndSetFormat = 'H:mm';
// length of day
const start = parseISO(data.Sunrise);
const end = parseISO(data.Sunset);
const durMins = differenceInMinutes(end, start);
const hours = Math.floor(durMins / 60);
const minutes = durMins - hours * 60;
let lengthofday = `${hours} h ${minutes} min`;
if (!isSameDay(start, end) && isSameYear(start, end)) {
// not same day, but is same year
sunriseAndSetFormat = 'd.M. H:mm';
lengthofday = null;
} else if (!isSameYear(start, end)) {
// not same year
sunriseAndSetFormat = 'd.M.yyyy H:mm';
lengthofday = null;
}
// format sunrise and set
const sunrise = data.Sunrise ? format(start, sunriseAndSetFormat) : '-';
const sunset = data.Sunset ? format(end, sunriseAndSetFormat) : '-';
return {
sunrise,
sunset,
lengthofday,
};
The text was updated successfully, but these errors were encountered:
Check how day length is calculated.
fmi.fi uses the following codes
The text was updated successfully, but these errors were encountered: