2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
10 #include <sys/types.h>
21 #define IN_RANGE( val, low, high ) ( (low) <= (val) && (val) <= (high) )
29 #define TIP( p ) ( (struct time_interval *) (p) )
30 #define NEW_TI() NEW( struct time_interval )
31 #define FREE_TI( tip ) FREE( tip )
35 * Returns TRUE if the current time is within at least one of the intervals
37 bool_int ti_current_time_check( const pset_h intervals )
44 (void) time( ¤t_time ) ;
45 tmp = localtime( ¤t_time ) ;
46 min_current = tmp->tm_hour * 60 + tmp->tm_min ;
48 for ( u = 0 ; u < pset_count( intervals ) ; u++ )
50 struct time_interval *tip ;
52 tip = TIP( pset_pointer( intervals, u ) ) ;
53 if ( IN_RANGE( min_current, tip->min_start, tip->min_end ) )
60 static int get_num( int *nump,
66 const char *func = "get_num" ;
69 for ( *nump = 0 ; isdigit( s[i] ) ; i++ )
75 if ( s[i] != stop_char )
77 parsemsg( LOG_ERR, func, "incorrect time interval" ) ;
81 if ( ! IN_RANGE( *nump, min_val, max_val ) )
83 parsemsg( LOG_ERR, func, "invalid time interval" ) ;
91 * Each interval should have the form:
95 status_e ti_add( pset_h iset, const char *interval_str )
97 struct time_interval *tip ;
103 const char *func = "add_interval" ;
105 while (interval_str[r] == ' ')
106 r++; /* Eat white space */
107 if ( ( p = get_num( &hours, 0, 23, interval_str+r, ':' ) ) == -1 )
110 r++; /* Get past : */
111 if ( ( p = get_num( &minutes, 0, 59, interval_str+r, '-' ) ) == -1 )
113 min_start = hours * 60 + minutes ;
116 r++; /* Get past - */
117 if ( ( p = get_num( &hours, 0, 23, interval_str+r, ':' ) ) == -1 )
120 r++; /* Get past : */
121 if ( get_num( &minutes, 0, 59, interval_str+r, NUL ) == -1 )
123 min_end = hours * 60 + minutes ;
124 if ( min_start >= min_end )
126 parsemsg( LOG_ERR, func, "invalid time interval: %s", interval_str ) ;
133 out_of_memory( func ) ;
136 tip->min_start = min_start ;
137 tip->min_end = min_end ;
138 if ( pset_add( iset, tip ) == NULL )
141 out_of_memory( func ) ;
148 void ti_dump( pset_h iset, int fd )
152 for ( u = 0 ; u < pset_count( iset ) ; u++ )
154 struct time_interval *tip = TIP( pset_pointer( iset, u ) ) ;
156 Sprint( fd, " %02d:%02d-%02d:%02d",
157 tip->min_start / 60, tip->min_start % 60,
158 tip->min_end / 60, tip->min_end % 60 ) ;
163 void ti_free( pset_h iset )
165 pset_apply( iset, free, NULL ) ;