5#ifndef FORESTHUB_UTIL_TICKER_HPP
6#define FORESTHUB_UTIL_TICKER_HPP
91 return Ticker(86400UL, kEpochSundayOffset - (
static_cast<long>(hour) * 3600L + minute * 60L));
100 return Ticker(604800UL, kEpochSundayOffset - (
static_cast<long>(weekday) * 86400L +
101 static_cast<long>(hour) * 3600L + minute * 60L));
139 last_slot_ = Slot(time);
153 if (!running_)
return false;
157 unsigned long elapsed = time - start_time_;
158 if (elapsed < period_)
return false;
159 missed_ticks_ = (elapsed / period_) - 1;
160 start_time_ += (missed_ticks_ + 1) * period_;
163 long current_slot = Slot(time);
164 if (current_slot <= last_slot_)
return false;
165 missed_ticks_ =
static_cast<unsigned long>(current_slot - last_slot_ - 1);
166 last_slot_ = current_slot;
170 if (max_fires_ > 0 && fire_count_ >= max_fires_) running_ =
false;
175 void Stop() { running_ =
false; }
195 unsigned long period()
const {
return period_; }
204 static constexpr long kEpochSundayOffset = 4L * 86400L;
210 long Slot(
unsigned long time)
const {
211 long val =
static_cast<long>(time) + tz_offset_sec_ + offset_;
212 long p =
static_cast<long>(period_);
213 return val / p - (val % p < 0);
216 unsigned long period_;
220 unsigned long start_time_;
222 unsigned long fire_count_;
223 unsigned long missed_ticks_;
224 unsigned long max_fires_;
unsigned long period() const
Configured period in time units.
Definition ticker.hpp:195
void SetTimezoneOffset(long tz_offset_sec)
Update timezone offset at runtime (e.g., after DST transition).
Definition ticker.hpp:179
unsigned long missed_ticks() const
Number of intervals skipped in the last Check() that fired.
Definition ticker.hpp:192
static Ticker Periodic(unsigned long interval)
Create a periodic ticker that fires at a fixed interval.
Definition ticker.hpp:68
static Ticker OneShot(unsigned long delay)
Create a one-shot timer that fires exactly once after a delay.
Definition ticker.hpp:78
static Ticker Hourly(int minute=0)
Create an hourly ticker that fires once per hour at a specific minute.
Definition ticker.hpp:107
Ticker & WithMaxFires(unsigned long n)
Set the maximum number of fires before auto-stopping (0 = unlimited).
Definition ticker.hpp:112
void Stop()
Stop the ticker. Call Start() to restart.
Definition ticker.hpp:175
static Ticker Weekly(int weekday, int hour, int minute=0)
Create a weekly ticker that fires once per week on a specific day and time.
Definition ticker.hpp:99
unsigned long fire_count() const
Number of times the ticker has fired since last Start().
Definition ticker.hpp:187
bool Check(unsigned long time)
Check if the ticker should fire at the given time.
Definition ticker.hpp:152
Ticker & WithTimezoneOffset(long tz_offset_sec)
Set timezone offset for calendar-based scheduling (Daily/Weekly/Hourly).
Definition ticker.hpp:123
Ticker(unsigned long period, long offset=0)
Construct a Ticker with custom period and offset.
Definition ticker.hpp:49
long tz_offset_sec() const
Configured timezone offset in seconds (0 if not set).
Definition ticker.hpp:198
bool running() const
Whether the ticker is currently running.
Definition ticker.hpp:184
static Ticker Daily(int hour, int minute=0)
Create a daily ticker that fires once per day at a specific hour and minute.
Definition ticker.hpp:90
void Start(unsigned long time)
Start (or restart) the ticker with the given time reference.
Definition ticker.hpp:135
Utility types: Optional polyfill, JSON Schema normalization, Ticker.
Top-level namespace for the ForestHub SDK.