Browse code

acc(k): Replace ftime() call with gettimeofday().

* ftime() is deprecated, switch to gettimeofday().
* Employ timeval structure (created from gettimeofday()) instead
of timeb structure.

Timo Reimann authored on 27/09/2011 21:46:14
Showing 1 changed files
... ...
@@ -44,7 +44,7 @@
44 44
 #include "acc_extra.h"
45 45
 #include "acc.h"
46 46
 
47
-#include <sys/timeb.h>
47
+#include <sys/time.h>
48 48
 
49 49
 #define TIME_STR_BUFFER_SIZE 20
50 50
 
... ...
@@ -56,9 +56,9 @@ static const str start_id = { "sz", 2};
56 56
 static const str end_id = { "ez", 2};
57 57
 static const str duration_id = { "d", 1};
58 58
 static const str zero_duration = { "0", 1};
59
-static const struct timeb time_error = {0,0,0,0};
59
+static const struct timeval time_error = {0,0};
60 60
 static const char time_separator = {'.'};
61
-static const int milliseconds_max = 1000;
61
+static const int milliseconds_max = 1000000;
62 62
 static const unsigned int time_buffer_length = 256;
63 63
 static const str empty_string = { "", 0};
64 64
 
... ...
@@ -71,13 +71,11 @@ static char cdr_type_array[ MAX_CDR_CORE + MAX_CDR_EXTRA];
71 71
 extern struct tm_binds tmb;
72 72
 
73 73
 /* compare two times */
74
-static int is_time_equal( struct timeb first_time,
75
-                          struct timeb second_time)
74
+static int is_time_equal( struct timeval first_time,
75
+                          struct timeval second_time)
76 76
 {
77
-    if( first_time.time == second_time.time &&
78
-        first_time.millitm == second_time.millitm &&
79
-        first_time.timezone == second_time.timezone &&
80
-        first_time.dstflag == second_time.dstflag )
77
+    if( first_time.tv_sec == second_time.tv_sec &&
78
+        first_time.tv_usec == second_time.tv_usec )
81 79
     {
82 80
         return 1;
83 81
     }
... ...
@@ -197,7 +195,7 @@ static int write_cdr( struct dlg_cell* dialog,
197 195
 }
198 196
 
199 197
 /* convert a string into a timeb struct */
200
-static struct timeb time_from_string( str* time_value)
198
+static struct timeval time_from_string( str* time_value)
201 199
 {    
202 200
     char* dot_address = NULL;
203 201
     int dot_position = -1;
... ...
@@ -239,17 +237,15 @@ static struct timeb time_from_string( str* time_value)
239 237
         return time_error;
240 238
     }
241 239
     
242
-    return (struct timeb) { atoi( zero_terminated_value),
243
-                            atoi( dot_address + 1),
244
-                            0,
245
-                            0};
240
+    return (struct timeval) { atoi( zero_terminated_value),
241
+                            atoi( dot_address + 1)};
246 242
 }
247 243
 
248 244
 /* set the duration in the dialog struct */
249 245
 static int set_duration( struct dlg_cell* dialog)
250 246
 {
251
-    struct timeb start_timeb = time_error;
252
-    struct timeb end_timeb = time_error;
247
+    struct timeval start_timeval = time_error;
248
+    struct timeval end_timeval = time_error;
253 249
     int milliseconds = -1;
254 250
     int seconds = -1;
255 251
     char buffer[ time_buffer_length];
... ...
@@ -262,35 +258,35 @@ static int set_duration( struct dlg_cell* dialog)
262 258
         return -1;
263 259
     }
264 260
 
265
-    start_timeb = time_from_string( dlgb.get_dlg_var( dialog, (str*)&start_id));
266
-    end_timeb  = time_from_string( dlgb.get_dlg_var( dialog, (str*)&end_id));
261
+    start_timeval = time_from_string( dlgb.get_dlg_var( dialog, (str*)&start_id));
262
+    end_timeval  = time_from_string( dlgb.get_dlg_var( dialog, (str*)&end_id));
267 263
 
268
-    if( is_time_equal( start_timeb, time_error) ||
269
-        is_time_equal( end_timeb, time_error))
264
+    if( is_time_equal( start_timeval, time_error) ||
265
+        is_time_equal( end_timeval, time_error))
270 266
     {
271 267
         LM_ERR( "failed to extract time from start or/and end-time\n");
272 268
         return -1;
273 269
     }
274 270
 
275
-    if( start_timeb.millitm >= milliseconds_max ||
276
-        end_timeb.millitm >= milliseconds_max)
271
+    if( start_timeval.tv_usec >= milliseconds_max ||
272
+        end_timeval.tv_usec >= milliseconds_max)
277 273
     {
278 274
         LM_ERR( "start-(%d) or/and end-time(%d) is out of the maximum of %d\n",
279
-                start_timeb.millitm,
280
-                end_timeb.millitm,
275
+                start_timeval.tv_usec,
276
+                end_timeval.tv_usec,
281 277
                 milliseconds_max);
282 278
         return -1;
283 279
     }
284 280
 
285
-    milliseconds = end_timeb.millitm < start_timeb.millitm ?
281
+    milliseconds = end_timeval.tv_usec < start_timeval.tv_usec ?
286 282
                                 ( milliseconds_max +
287
-                                  end_timeb.millitm -
288
-                                  start_timeb.millitm) :
289
-                                ( end_timeb.millitm - start_timeb.millitm);
283
+                                  end_timeval.tv_usec -
284
+                                  start_timeval.tv_usec) :
285
+                                ( end_timeval.tv_usec - start_timeval.tv_usec);
290 286
 
291
-    seconds = end_timeb.time -
292
-              start_timeb.time -
293
-              ( end_timeb.millitm < start_timeb.millitm ? 1 : 0);
287
+    seconds = end_timeval.tv_sec -
288
+              start_timeval.tv_sec -
289
+              ( end_timeval.tv_usec < start_timeval.tv_usec ? 1 : 0);
294 290
 
295 291
     if( seconds < 0)
296 292
     {
... ...
@@ -336,7 +332,7 @@ static int set_duration( struct dlg_cell* dialog)
336 332
 static int set_start_time( struct dlg_cell* dialog)
337 333
 {
338 334
     char buffer[ time_buffer_length];
339
-    struct timeb current_time = time_error;
335
+    struct timeval current_time;
340 336
     int buffer_length = -1;
341 337
     str start_time = empty_string;
342 338
 
... ...
@@ -346,7 +342,7 @@ static int set_start_time( struct dlg_cell* dialog)
346 342
         return -1;
347 343
     }
348 344
 
349
-    if( ftime( &current_time) < 0)
345
+    if( gettimeofday( &current_time, NULL) < 0)
350 346
     {
351 347
         LM_ERR( "failed to get current time!\n");
352 348
         return -1;
... ...
@@ -355,9 +351,9 @@ static int set_start_time( struct dlg_cell* dialog)
355 351
     buffer_length = snprintf( buffer,
356 352
                               time_buffer_length,
357 353
                               "%d%c%03d",
358
-                              (int)current_time.time,
354
+                              (int)current_time.tv_sec,
359 355
                               time_separator,
360
-                              (int)current_time.millitm);
356
+                              (int)current_time.tv_usec);
361 357
 
362 358
     if( buffer_length < 0)
363 359
     {
... ...
@@ -398,7 +394,7 @@ static int set_start_time( struct dlg_cell* dialog)
398 394
 static int set_end_time( struct dlg_cell* dialog)
399 395
 {
400 396
     char buffer[ time_buffer_length];
401
-    struct timeb current_time = time_error;
397
+    struct timeval current_time;
402 398
     int buffer_length = -1;
403 399
     str end_time = empty_string;
404 400
 
... ...
@@ -408,7 +404,7 @@ static int set_end_time( struct dlg_cell* dialog)
408 404
         return -1;
409 405
     }
410 406
 
411
-    if( ftime( &current_time) < 0)
407
+    if( gettimeofday( &current_time, NULL) < 0)
412 408
     {
413 409
         LM_ERR( "failed to set time!\n");
414 410
         return -1;
... ...
@@ -417,9 +413,9 @@ static int set_end_time( struct dlg_cell* dialog)
417 413
     buffer_length = snprintf( buffer,
418 414
                               time_buffer_length,
419 415
                               "%d%c%03d",
420
-                              (int)current_time.time,
416
+                              (int)current_time.tv_sec,
421 417
                               time_separator,
422
-                              (int)current_time.millitm);
418
+                              (int)current_time.tv_usec);
423 419
 
424 420
     if( buffer_length < 0)
425 421
     {