@@ -234,6 +234,8 @@ struct Match {
234
234
Location location;
235
235
Match match;
236
236
237
+ auto next_update = event::DefaultTimePoint;
238
+
237
239
void setup ();
238
240
239
241
void reset () {
@@ -825,41 +827,47 @@ bool update_schedule(Schedule& schedule) {
825
827
}
826
828
827
829
bool needs_update (datetime::Clock::time_point time_point) {
828
- return (match.rising .next < time_point)
829
- || (match.setting .next < time_point);
830
+ const auto next = event::is_valid (next_update);
831
+ if (next) {
832
+ return event::less (next_update, time_point);
833
+ }
834
+
835
+ return event::less (match.rising .next , time_point)
836
+ || event::less (match.setting .next , time_point);
830
837
}
831
838
832
839
template <typename T>
833
- void delta_compare (tm & out, datetime::Clock::time_point, T);
840
+ datetime::Clock::time_point delta_compare (tm & out, datetime::Clock::time_point, T);
834
841
835
- void update (const datetime::Clock::time_point& , const tm & today) {
842
+ void update (datetime::Clock::time_point, const tm & today) {
836
843
const auto result = sun::sunrise_sunset (location, today);
837
844
update_event_match (match.rising , result.sunrise );
838
845
update_event_match (match.setting , result.sunset );
839
846
}
840
847
841
848
template <typename T>
842
- void update (const datetime::Clock::time_point& time_point, const tm & today, T compare) {
849
+ void update (datetime::Clock::time_point time_point, const tm & today, T compare) {
843
850
auto result = sun::sunrise_sunset (location, today);
844
- if (!event::is_valid (result.sunrise ) || !event::is_valid (result.sunset )) {
845
- return ;
846
- }
847
851
848
- if (compare (time_point, result.sunrise ) || compare (time_point, result.sunset )) {
849
- tm tmp;
852
+ const auto reset_sunrise =
853
+ !event::is_valid (result.sunrise ) || compare (time_point, result.sunrise );
854
+
855
+ const auto reset_sunset =
856
+ !event::is_valid (result.sunset ) || compare (time_point, result.sunset );
857
+
858
+ next_update = event::DefaultTimePoint;
859
+
860
+ tm tmp;
861
+ if (reset_sunrise || reset_sunset) {
850
862
std::memcpy (&tmp, &today, sizeof (tmp));
851
- delta_compare (tmp, time_point, compare);
863
+ next_update = delta_compare (tmp, time_point, compare);
852
864
853
865
const auto other = sun::sunrise_sunset (location, tmp);
854
- if (!event::is_valid (other.sunrise ) || !event::is_valid (other.sunset )) {
855
- return ;
856
- }
857
-
858
- if (compare (time_point, result.sunrise )) {
866
+ if (reset_sunrise && event::is_valid (other.sunrise )) {
859
867
result.sunrise = other.sunrise ;
860
868
}
861
869
862
- if (compare (time_point, result .sunset )) {
870
+ if (reset_sunset && event::is_valid (other .sunset )) {
863
871
result.sunset = other.sunset ;
864
872
}
865
873
}
@@ -881,15 +889,16 @@ String format_match(const EventMatch& match) {
881
889
// round to minutes when doing so as well, since std::greater<> would compare seconds
882
890
struct CheckCompare {
883
891
bool operator ()(const event::time_point& lhs, const event::time_point& rhs) {
884
- return to_minutes (lhs) > to_minutes ( rhs);
892
+ return event::greater (lhs, rhs);
885
893
}
886
894
};
887
895
888
896
template <>
889
- void delta_compare (tm & out, datetime::Clock::time_point time_point, CheckCompare) {
890
- datetime::delta_utc (
891
- out, time_point.time_since_epoch (),
892
- datetime::Days{ 1 });
897
+ datetime::Clock::time_point delta_compare (tm & out, datetime::Clock::time_point time_point, CheckCompare) {
898
+ return datetime::make_time_point (
899
+ datetime::delta_utc (
900
+ out, time_point.time_since_epoch (),
901
+ datetime::Days{ 1 }));
893
902
}
894
903
895
904
void update_after (const datetime::Context& ctx) {
0 commit comments