buelowp / sunset

Calculate Sunrise and Sunset and the phase of the moon accurately based on date and geographic position
GNU General Public License v2.0
127 stars 36 forks source link

Negative values for moonphase #41

Closed tytoalba99 closed 2 years ago

tytoalba99 commented 2 years ago

hi,

I get negative values for moonphase. (i.e -6). Sunset and Sunrise calculations are OK. Environment ESP8266 (Lolin D1 mini)

15:06:48.120 -> Sunrise at 5:44, Sunset at 20:50 moonphase: -6

Regards

buelowp commented 2 years ago

Can you give me some testable code and tell me which release?

tytoalba99 commented 2 years ago
sun.setPosition(LATITUDE, LONGITUDE, TIMEZONE);
sun.setCurrentDate(time.year(),time.month(), time.day());
    // If you have daylight savings time, make sure you set the timezone appropriately as well
    sun.setTZOffset(TIMEZONE);
    double sunrise = sun.calcSunrise();
    double sunset = sun.calcSunset();
    int moonphase = sun.moonPhase();

    String salidasol = String(int(sunrise/60))+":"+twoDigits(fmod(sunrise,60));
    String puestasol = String(int(sunset/60))+":"+twoDigits(fmod(sunset,60));
    int minutoactual = (time.hour()*60 + time.minute());

    Serial.print("Sunrise at ");
    Serial.print(salidasol);
    Serial.print(", Sunset at ");
    Serial.print(puestasol);
    Serial.print("  Luna: ");
    Serial.print(moonphase);
    Serial.print("  minuto Actual: ");
    Serial.println(minutoactual);
    Serial.println("\n");

Current version 1.1.6

buelowp commented 2 years ago

OK, this really isn't a bug, but it is confusing. The time on your system is empty, so time_t == 0 which the calculation cannot manage, and that causes the return value to be incorrect. This is a good error code though and I'll add it to the docs. Because the moonphase uses the current time, you will need to make sure

time_t t = std::time(0);

can return a positive real value. You can also set the value you want std::time to return by calling moonphase(int epoch) with a positive value. Note, this calculation cannot work for times before the Unix epoch, and not 0 either, it's not that sophisticated.

tytoalba99 commented 2 years ago

Solved int moonphase = sun.moonPhase(time.unixtime()); thank you.