Skip to content

Commit

Permalink
Fix: Day period is only calculated once
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 16, 2023
1 parent 527c2ea commit 6f685a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 0 additions & 1 deletion include/SunPosition.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class SunPositionClass {
bool getDoRecalc();

SunSet _sun;
bool _isDayPeriod = true;
bool _isSunsetAvailable = true;
uint32_t _sunriseMinutes = 0;
uint32_t _sunsetMinutes = 0;
Expand Down
13 changes: 8 additions & 5 deletions src/SunPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ void SunPositionClass::loop()

bool SunPositionClass::isDayPeriod()
{
return _isDayPeriod;
if (!_isValidInfo) {
return true;
}

struct tm timeinfo;
getLocalTime(&timeinfo, 5);
uint32_t minutesPastMidnight = timeinfo.tm_hour * 60 + timeinfo.tm_min;
return (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes);
}

bool SunPositionClass::isSunsetAvailable()
Expand Down Expand Up @@ -73,7 +80,6 @@ void SunPositionClass::updateSunData()
setDoRecalc(false);

if (!gotLocalTime) {
_isDayPeriod = true;
_sunriseMinutes = 0;
_sunsetMinutes = 0;
_isValidInfo = false;
Expand Down Expand Up @@ -108,7 +114,6 @@ void SunPositionClass::updateSunData()
// If no sunset/sunrise exists (e.g. astronomical calculation in summer)
// assume it's day period
if (std::isnan(sunriseRaw) || std::isnan(sunsetRaw)) {
_isDayPeriod = true;
_isSunsetAvailable = false;
_sunriseMinutes = 0;
_sunsetMinutes = 0;
Expand All @@ -118,9 +123,7 @@ void SunPositionClass::updateSunData()

_sunriseMinutes = static_cast<int>(sunriseRaw);
_sunsetMinutes = static_cast<int>(sunsetRaw);
uint32_t minutesPastMidnight = timeinfo.tm_hour * 60 + timeinfo.tm_min;

_isDayPeriod = (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes);
_isSunsetAvailable = true;
_isValidInfo = true;
}
Expand Down

0 comments on commit 6f685a4

Please sign in to comment.