Get Sunrise & Sunset for a Specific Date (#4712)

* Update real_time_clock.cpp

* Update real_time_clock.h

* Update sun.h

* Update sun.h

* Update sun.h

* Enable the sunAtLocation to be used externally

* Enable the sunAtLocation to be used externally

* Update sun.h

* Update sun.h

* update

* update

* update to only use one function

* Update sun.h

* Update sun.cpp
This commit is contained in:
Rebbe Pod 2023-04-23 16:44:35 -04:00 committed by GitHub
parent 327cd662b4
commit 0a95f116fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View file

@ -287,18 +287,17 @@ HorizontalCoordinate Sun::calc_coords_() {
*/ */
return sun.true_coordinate(m); return sun.true_coordinate(m);
} }
optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) { optional<time::ESPTime> Sun::calc_event_(time::ESPTime date, bool rising, double zenith) {
SunAtLocation sun{location_}; SunAtLocation sun{location_};
auto now = this->time_->utcnow(); if (!date.is_valid())
if (!now.is_valid())
return {}; return {};
// Calculate UT1 timestamp at 0h // Calculate UT1 timestamp at 0h
auto today = now; auto today = date;
today.hour = today.minute = today.second = 0; today.hour = today.minute = today.second = 0;
today.recalc_timestamp_utc(); today.recalc_timestamp_utc();
auto it = sun.event(rising, today, zenith); auto it = sun.event(rising, today, zenith);
if (it.has_value() && it->timestamp < now.timestamp) { if (it.has_value() && it->timestamp < date.timestamp) {
// We're calculating *next* sunrise/sunset, but calculated event // We're calculating *next* sunrise/sunset, but calculated event
// is today, so try again tomorrow // is today, so try again tomorrow
time_t new_timestamp = today.timestamp + 24 * 60 * 60; time_t new_timestamp = today.timestamp + 24 * 60 * 60;
@ -307,9 +306,19 @@ optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) {
} }
return it; return it;
} }
optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) {
auto it = Sun::calc_event_(this->time_->utcnow(), rising, zenith);
return it;
}
optional<time::ESPTime> Sun::sunrise(double elevation) { return this->calc_event_(true, 90 - elevation); } optional<time::ESPTime> Sun::sunrise(double elevation) { return this->calc_event_(true, 90 - elevation); }
optional<time::ESPTime> Sun::sunset(double elevation) { return this->calc_event_(false, 90 - elevation); } optional<time::ESPTime> Sun::sunset(double elevation) { return this->calc_event_(false, 90 - elevation); }
optional<time::ESPTime> Sun::sunrise(time::ESPTime date, double elevation) {
return this->calc_event_(date, true, 90 - elevation);
}
optional<time::ESPTime> Sun::sunset(time::ESPTime date, double elevation) {
return this->calc_event_(date, false, 90 - elevation);
}
double Sun::elevation() { return this->calc_coords_().elevation; } double Sun::elevation() { return this->calc_coords_().elevation; }
double Sun::azimuth() { return this->calc_coords_().azimuth; } double Sun::azimuth() { return this->calc_coords_().azimuth; }

View file

@ -59,6 +59,8 @@ class Sun {
optional<time::ESPTime> sunrise(double elevation); optional<time::ESPTime> sunrise(double elevation);
optional<time::ESPTime> sunset(double elevation); optional<time::ESPTime> sunset(double elevation);
optional<time::ESPTime> sunrise(time::ESPTime date, double elevation);
optional<time::ESPTime> sunset(time::ESPTime date, double elevation);
double elevation(); double elevation();
double azimuth(); double azimuth();
@ -66,6 +68,7 @@ class Sun {
protected: protected:
internal::HorizontalCoordinate calc_coords_(); internal::HorizontalCoordinate calc_coords_();
optional<time::ESPTime> calc_event_(bool rising, double zenith); optional<time::ESPTime> calc_event_(bool rising, double zenith);
optional<time::ESPTime> calc_event_(time::ESPTime date, bool rising, double zenith);
time::RealTimeClock *time_; time::RealTimeClock *time_;
internal::GeoLocation location_; internal::GeoLocation location_;