Main Page   Compound List   File List   Compound Members   File Members  

FireTimer.c

Go to the documentation of this file.
00001 
00025 #include "FireTimer.h"
00026 
00027 /* 0 based mo array...                J   F   M   A   M   J   J   A   S   O   N   D */
00028 static const int sdays_in_month [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00029 
00030 FireTimer * InitFireTimer(int sim_start_yr, int sim_start_mo, int sim_start_dy, int sim_start_hr,
00031                           int sim_end_yr, int sim_end_mo, int sim_end_dy, int sim_end_hr)   {
00032     FireTimer * ft  = NULL;
00033 
00034     if ( sim_start_yr > sim_end_yr )    {
00035         ERR_ERROR_CONTINUE("Unable to initialize FireTimer, end year precedes start year. \n", ERR_ESANITY);
00036         return ft;
00037         }
00038     if ( sim_start_mo > FIRE_TIMER_MAX_MO_YEAR || sim_end_mo > FIRE_TIMER_MAX_MO_YEAR ) {
00039         ERR_ERROR_CONTINUE("Unable to initialize FireTimer, month not in range 1 to 12. \n", ERR_ESANITY);
00040         return ft;
00041         }
00042     if ( FireTimerGetDaysInMonth(sim_start_mo) < sim_start_dy || FireTimerGetDaysInMonth(sim_end_mo) < sim_end_dy ) {
00043         ERR_ERROR_CONTINUE("Unable to initialize FireTimer, day exceeds days in month. \n", ERR_ESANITY);
00044         return ft;
00045         }
00046     if ( sim_start_hr > FIRE_TIMER_MAX_HOUR_DAY || sim_end_hr > FIRE_TIMER_MAX_HOUR_DAY )   {
00047         ERR_ERROR_CONTINUE("Unable to initialize FireTimer, hour not in range 0 to 23. \n", ERR_ESANITY);
00048         return ft;
00049         }   
00050     if ( (ft = (FireTimer *) malloc(sizeof(FireTimer))) == NULL )   {
00051         ERR_ERROR_CONTINUE("Unable to initialize FireTimer, memory allocation failed. \n", ERR_ENOMEM);
00052         return ft;  
00053         }
00054 
00055     /* simulation start time */
00056     ft->sim_start_yr    = sim_start_yr;
00057     ft->sim_start_mo    = sim_start_mo;
00058     ft->sim_start_dy    = sim_start_dy;
00059     ft->sim_start_hr    = sim_start_hr;
00060     /* simulation end time */   
00061     ft->sim_end_yr      = sim_end_yr;
00062     ft->sim_end_mo      = sim_end_mo;
00063     ft->sim_end_dy      = sim_end_dy;       
00064     ft->sim_end_hr      = sim_end_hr;
00065     /* current simulation time */   
00066     ft->sim_cur_yr      = sim_start_yr;
00067     ft->sim_cur_mo      = sim_start_mo; 
00068     ft->sim_cur_dy      = sim_start_dy;
00069     ft->sim_cur_hr      = sim_start_hr;
00070     ft->sim_cur_secs    = FIRE_TIMER_HOUR_TO_SECS(sim_start_hr);
00071     
00072     return ft;      
00073     }
00074 
00075 int FireTimerIncrementSeconds(FireTimer * ft, int secs) {   
00076     if ( ft == NULL )   {
00077         ERR_ERROR("Unable to increment simulation seconds, FireTimer not initialized. \n", ERR_EINVAL);     
00078         }
00079     ft->sim_cur_secs += secs;
00080     /* test if hour has been exceeded */
00081     if ( FIRE_TIMER_SECS_TO_HOUR(ft->sim_cur_secs) > ft->sim_cur_hr )   {
00082         ft->sim_cur_hr += 1;
00083         /* test if day has been exceeded */
00084         if ( ft->sim_cur_hr > FIRE_TIMER_MAX_HOUR_DAY ) {
00085             ft->sim_cur_secs -= FIRE_TIMER_SECS_PER_DAY;        
00086             ft->sim_cur_hr = FIRE_TIMER_MIN_HOUR_DAY;
00087             /* if month has been exceeded, and increment mo and dy */
00088             if ( FireTimerGetDaysInMonth(ft->sim_cur_mo) == ft->sim_cur_dy) {
00089                 ft->sim_cur_mo += 1;
00090                 ft->sim_cur_dy = 1;
00091                 /* test if year has been exceeded */
00092                 if ( ft->sim_cur_mo > FIRE_TIMER_MAX_MO_YEAR )  {
00093                     ft->sim_cur_yr += 1;
00094                     ft->sim_cur_mo = FIRE_TIMER_MIN_MO_YEAR;
00095                     ft->sim_cur_dy = 1;
00096                     ft->sim_cur_hr = FIRE_TIMER_MIN_HOUR_DAY;
00097                     }
00098                 }
00099             /* only increment day */
00100             else    {
00101                 ft->sim_cur_dy += 1;
00102                 }
00103             }
00104         }
00105     
00106     return ERR_SUCCESS;
00107     }
00108     
00109 int FireTimerIsSimCurYearTimeExpired(FireTimer * ft)    {
00110     if ( ft->sim_cur_mo < ft->sim_end_mo )
00111         return 0;
00112     if ( ft->sim_cur_dy < ft->sim_end_dy )
00113         return 0;
00114     if ( ft->sim_cur_hr < ft->sim_end_hr )
00115         return 0;
00116     return 1;
00117     }
00118     
00119 int FireTimerIsSimTimeExpired(FireTimer * ft)   {
00120     if ( ft->sim_cur_yr < ft->sim_end_yr )
00121         return 0;
00122     if ( ft->sim_cur_mo < ft->sim_end_mo )
00123         return 0;
00124     if ( ft->sim_cur_dy < ft->sim_end_dy )
00125         return 0;
00126     if ( ft->sim_cur_hr < ft->sim_end_hr )
00127         return 0;
00128     return 1;
00129     }
00130 
00131 int FireTimerGetDaysInMonth(int mo)     {
00132     return sdays_in_month[mo - 1];
00133     }
00134 
00135 int FireTimerGetDaysDifftime(int mo1, int dy1, int mo2, int dy2)    {
00136     int days_diff = 0;
00137     
00138     /* number of days in first month */
00139     days_diff += FireTimerGetDaysInMonth(mo1) - dy1;
00140     mo1++;
00141     
00142     /* number of days not in first or last months */    
00143     for( ; mo1 < mo2 && mo1 <=FIRE_TIMER_MAX_MO_YEAR; mo1++)    {
00144         days_diff += FireTimerGetDaysInMonth(mo1);
00145         }
00146     
00147     /* nunber of days in last month */
00148     days_diff += dy2;
00149     
00150     return days_diff;
00151     }
00152 
00153  void FreeFireTimer(FireTimer * fs) {
00154     if ( fs != NULL )   {
00155         free(fs);
00156         }
00157     
00158     fs = NULL;
00159     return; 
00160     }
00161     
00162 /* end of FireTimer.c */

Generated at Fri Jun 22 00:46:51 2001 for HFire by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000