… | |
… | |
2225 | C<ev_timer>, which would still trigger roughly 10 seconds after starting |
2225 | C<ev_timer>, which would still trigger roughly 10 seconds after starting |
2226 | it, as it uses a relative timeout). |
2226 | it, as it uses a relative timeout). |
2227 | |
2227 | |
2228 | C<ev_periodic> watchers can also be used to implement vastly more complex |
2228 | C<ev_periodic> watchers can also be used to implement vastly more complex |
2229 | timers, such as triggering an event on each "midnight, local time", or |
2229 | timers, such as triggering an event on each "midnight, local time", or |
2230 | other complicated rules. This cannot be done with C<ev_timer> watchers, as |
2230 | other complicated rules. This cannot easily be done with C<ev_timer> |
2231 | those cannot react to time jumps. |
2231 | watchers, as those cannot react to time jumps. |
2232 | |
2232 | |
2233 | As with timers, the callback is guaranteed to be invoked only when the |
2233 | As with timers, the callback is guaranteed to be invoked only when the |
2234 | point in time where it is supposed to trigger has passed. If multiple |
2234 | point in time where it is supposed to trigger has passed. If multiple |
2235 | timers become ready during the same loop iteration then the ones with |
2235 | timers become ready during the same loop iteration then the ones with |
2236 | earlier time-out values are invoked before ones with later time-out values |
2236 | earlier time-out values are invoked before ones with later time-out values |
… | |
… | |
2322 | |
2322 | |
2323 | NOTE: I<< This callback must always return a time that is higher than or |
2323 | NOTE: I<< This callback must always return a time that is higher than or |
2324 | equal to the passed C<now> value >>. |
2324 | equal to the passed C<now> value >>. |
2325 | |
2325 | |
2326 | This can be used to create very complex timers, such as a timer that |
2326 | This can be used to create very complex timers, such as a timer that |
2327 | triggers on "next midnight, local time". To do this, you would calculate the |
2327 | triggers on "next midnight, local time". To do this, you would calculate |
2328 | next midnight after C<now> and return the timestamp value for this. How |
2328 | the next midnight after C<now> and return the timestamp value for |
2329 | you do this is, again, up to you (but it is not trivial, which is the main |
2329 | this. Here is a (completely untested, no error checking) example on how to |
2330 | reason I omitted it as an example). |
2330 | do this: |
|
|
2331 | |
|
|
2332 | #include <time.h> |
|
|
2333 | |
|
|
2334 | static ev_tstamp |
|
|
2335 | my_rescheduler (ev_periodic *w, ev_tstamp now) |
|
|
2336 | { |
|
|
2337 | time_t tnow = (time_t)now; |
|
|
2338 | struct tm tm; |
|
|
2339 | localtime_r (&tnow, &tm); |
|
|
2340 | |
|
|
2341 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; // midnight current day |
|
|
2342 | ++tm.tm_mday; // midnight next day |
|
|
2343 | |
|
|
2344 | return mktime (&tm); |
|
|
2345 | } |
|
|
2346 | |
|
|
2347 | Note: this code might run into trouble on days that have more then two |
|
|
2348 | midnights (beginning and end). |
2331 | |
2349 | |
2332 | =back |
2350 | =back |
2333 | |
2351 | |
2334 | =item ev_periodic_again (loop, ev_periodic *) |
2352 | =item ev_periodic_again (loop, ev_periodic *) |
2335 | |
2353 | |