Code

fix libwrap and libdbi detection logic
[rrdtool-all.git] / contrib / trytime / trytime.c
1 #include <time.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <rrd_tool.h>
8 #ifndef WANT_AT_STYLE_TIMESPEC
9 #define WANT_AT_STYLE_TIMESPEC
10 #endif
12 #define BUF_LEN 128
14 static char soption[BUF_LEN];
15 static char eoption[BUF_LEN];
17 int main ( int ac, char **av )
18 {
19   static struct option long_options[] =
20   {
21      {"start",      required_argument, 0, 's'},
22      {"end",        required_argument, 0, 'e'},
23      {0,0,0,0}};
24   int option_index = 0;
25   int opt;
26    
27   time_t start_tmp, end_tmp, Now = time(NULL);
28   char tim_b[200];
29   
30   struct time_value start_tv, end_tv;
31   char *parsetime_error = NULL;
32   
33   /* default values */
34   parsetime("end-24h", &start_tv);
35   parsetime("now", &end_tv);
37   if( ac < 2 )
38     {
39     printf( "usage: %s [--start|-s start] [--end|-e end]\n"
40             "\n"
41             "In plain English, this means that to time specification try\n"
42             "a single time specification (just like in the rrdtool create)\n"
43             "you can use the first form, while to try two of them at once\n"
44             "(just like in rrdtool graph or fetch) you need the seconf form\n",
45             av[0] );
46     exit(0);
47     }
48   
49   printf( "The time now is: %s\n", ctime(&Now) );
50   
51   while(1){
52         opt = getopt_long(ac, av, "s:e:", long_options, &option_index);
53     
54         if (opt == EOF)  
55             break;
56         
57         switch(opt)
58         {
59         case 's': 
60             strncpy( soption, optarg, BUF_LEN );
61             if ((parsetime_error = parsetime(optarg, &start_tv))) {
62                 fprintf( stderr, "ERROR: start time: %s\n", parsetime_error );
63                 exit(1);
64             }
65             
66             break;
67         case 'e': 
68             strncpy( eoption, optarg, BUF_LEN );
69             if ((parsetime_error = parsetime(optarg, &end_tv))) {
70                 fprintf( stderr, "ERROR: end time: %s\n", parsetime_error );
71                 exit(1);
72             }       
73             break;
74         }
75   }
76   
77   if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
78       printf("ERROR: %s\n",rrd_get_error());
79       rrd_clear_error();
80       exit(1);
81   }
82   
83   strftime(tim_b,100,"%c %Z",localtime(&start_tmp));
84   if( *soption )
85       printf( "Start time was specified as: '%s',\n"
86               "for me this means: %s (or %ld sec since epoch)\n\n", 
87               soption, tim_b, start_tmp );
88     else
89       printf( "Start time was not specified, default value will be used (end-24h)\n"
90               "for me this means: %s (or %ld sec since epoch)\n\n",
91               tim_b, start_tmp );
92     
93   strftime(tim_b,100,"%c %Z",localtime(&end_tmp));
94   if( *eoption )
95       printf( "End time was specified as: '%s',\n"
96               "for me this means: %s (or %ld sec since epoch)\n", 
97               eoption, tim_b, end_tmp );
98   else
99       printf( "End time was not specified, default value will be used (now)\n"
100               "for me this means: %s (or %ld sec since epoch)\n\n",
101               tim_b, end_tmp );
102   exit(0);