Time

mg_millis()

uint64_t mg_millis(void);

Return current uptime in milliseconds.

Parameters: None

Return value: Current uptime

Usage example:

uint64_t uptime = mg_millis();

mg_now()

uint64_t mg_now(void);

Return current time in milliseconds, requires an SNTP server connection (see mg_sntp_connect())

Parameters: None

Return value: If an SNTP server connection has been configured, returns current time. Otherwise, returns current uptime just like mg_millis()

Usage example:

mg_sntp_connect(mgr&, NULL /* connect to time.google.com */, NULL, NULL);
...
uint64_t curtime = mg_now();

mg_timer_expired()

bool mg_timer_expired(uint64_t *t, uint64_t period, uint64_t now);

Parameters:

  • t - Pointer to a the timer value
  • period - timer interval
  • now - current time

Return true if a given timer t has expired: now >= *t, false otherwise. If the timer has expired, the t is advanced by the period.

Usage example:

uint64_t timer = 0, period = 500;  // Milliseconds
for (;;) {
  if (mg_timer_expired(&timer, period, mg_now())) {
    MG_INFO(("Hi!"));   // Print a message every 1/2 second
  }
  mg_mgr_poll(&mgr, 10);
}