Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in day length calculation #754

Open
AnttiRumpunen opened this issue Jan 14, 2025 · 0 comments
Open

Error in day length calculation #754

AnttiRumpunen opened this issue Jan 14, 2025 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@AnttiRumpunen
Copy link
Contributor

Image

Image

Check how day length is calculated.

fmi.fi uses the following codes

 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,
    };
@AnttiRumpunen AnttiRumpunen added the bug Something isn't working label Jan 14, 2025
@AnttiRumpunen AnttiRumpunen changed the title Error in day lenght calculation Error in day length calculation Jan 14, 2025
@AnttiRumpunen AnttiRumpunen added this to the 5.0.0 milestone Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant