00001 /* 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * A simple class that can trigger an event once per day. 00006 * Presently has a one-hour granularity, but that can be extended one 00007 * day when someone cares. 00008 * 00009 */ 00010 00011 #include "wvstream.h" 00012 #include "wvdailyevent.h" 00013 00014 #include <time.h> 00015 #include <sys/time.h> 00016 #include <unistd.h> 00017 00018 WvDailyEvent::WvDailyEvent( int _first_hour, int _num_per_day ) 00019 /*******************************************************/ 00020 : first_hour( _first_hour ), num_per_day( _num_per_day ) 00021 { 00022 need_reset = false; 00023 last_hour = -1; 00024 last_minute = -1; 00025 } 00026 00027 bool WvDailyEvent::pre_select( SelectInfo& si ) 00028 /***********************************************/ 00029 // we're "ready" if the time just changed to "first_hour" o'clock, 00030 // OR if the time just changed to "first_hour" o'clock plus a multiple of 00031 // 24*60 / num_per_day minutes. 00032 { 00033 time_t now; 00034 struct tm * tnow; 00035 00036 now = time( NULL ); 00037 tnow = localtime( &now ); 00038 00039 // for a specific hour 00040 if( tnow->tm_hour == first_hour ) { 00041 if( (first_hour-1) % 24 == last_hour ) 00042 need_reset = true; 00043 } 00044 last_hour = tnow->tm_hour; 00045 00046 // for a number of times a day 00047 // use the daily "first_hour" as an offset. (if first_hour is 3, and 00048 // num_per_day is 2, we want to tick at 3 am and 3 pm.) 00049 int this_minute = ( ( tnow->tm_hour - first_hour )%24 )*60 + tnow->tm_min; 00050 if( num_per_day ) { 00051 int min_between = 24*60 / num_per_day; 00052 if( this_minute % min_between == 0 ) { 00053 if( last_minute != this_minute ) 00054 need_reset = true; 00055 } 00056 } 00057 last_minute = this_minute; 00058 00059 return( need_reset ); 00060 } 00061 00062 bool WvDailyEvent::post_select( SelectInfo& si ) 00063 /*******************************************/ 00064 { 00065 return( need_reset ); 00066 } 00067 00068 void WvDailyEvent::execute() 00069 /**************************/ 00070 { 00071 WvStream::execute(); 00072 reset(); 00073 } 00074 00075 void WvDailyEvent::reset() 00076 /************************/ 00077 { 00078 need_reset = false; 00079 } 00080 00081 bool WvDailyEvent::isok() const 00082 /*****************************/ 00083 { 00084 return( true ); 00085 } 00086 00087 void WvDailyEvent::configure( int _first_hour, int _num_per_day ) 00088 /***************************************************************/ 00089 { 00090 first_hour = _first_hour; 00091 num_per_day = _num_per_day; 00092 }