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 197
 }
198 198
 
199 199
 /* convert a string into a timeb struct */
200
-static struct timeb time_from_string( str* time_value)
200
+static struct timeval time_from_string( str* time_value)
201 201
 {    
202 202
     char* dot_address = NULL;
203 203
     int dot_position = -1;
... ...
@@ -239,17 +237,15 @@ static struct timeb time_from_string( str* time_value)
239 239
         return time_error;
240 240
     }
241 241
     
242
-    return (struct timeb) { atoi( zero_terminated_value),
243
-                            atoi( dot_address + 1),
244
-                            0,
245
-                            0};
242
+    return (struct timeval) { atoi( zero_terminated_value),
243
+                            atoi( dot_address + 1)};
246 244
 }
247 245
 
248 246
 /* set the duration in the dialog struct */
249 247
 static int set_duration( struct dlg_cell* dialog)
250 248
 {
251
-    struct timeb start_timeb = time_error;
252
-    struct timeb end_timeb = time_error;
249
+    struct timeval start_timeval = time_error;
250
+    struct timeval end_timeval = time_error;
253 251
     int milliseconds = -1;
254 252
     int seconds = -1;
255 253
     char buffer[ time_buffer_length];
... ...
@@ -262,35 +258,35 @@ static int set_duration( struct dlg_cell* dialog)
262 262
         return -1;
263 263
     }
264 264
 
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));
265
+    start_timeval = time_from_string( dlgb.get_dlg_var( dialog, (str*)&start_id));
266
+    end_timeval  = time_from_string( dlgb.get_dlg_var( dialog, (str*)&end_id));
267 267
 
268
-    if( is_time_equal( start_timeb, time_error) ||
269
-        is_time_equal( end_timeb, time_error))
268
+    if( is_time_equal( start_timeval, time_error) ||
269
+        is_time_equal( end_timeval, time_error))
270 270
     {
271 271
         LM_ERR( "failed to extract time from start or/and end-time\n");
272 272
         return -1;
273 273
     }
274 274
 
275
-    if( start_timeb.millitm >= milliseconds_max ||
276
-        end_timeb.millitm >= milliseconds_max)
275
+    if( start_timeval.tv_usec >= milliseconds_max ||
276
+        end_timeval.tv_usec >= milliseconds_max)
277 277
     {
278 278
         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,
279
+                start_timeval.tv_usec,
280
+                end_timeval.tv_usec,
281 281
                 milliseconds_max);
282 282
         return -1;
283 283
     }
284 284
 
285
-    milliseconds = end_timeb.millitm < start_timeb.millitm ?
285
+    milliseconds = end_timeval.tv_usec < start_timeval.tv_usec ?
286 286
                                 ( milliseconds_max +
287
-                                  end_timeb.millitm -
288
-                                  start_timeb.millitm) :
289
-                                ( end_timeb.millitm - start_timeb.millitm);
287
+                                  end_timeval.tv_usec -
288
+                                  start_timeval.tv_usec) :
289
+                                ( end_timeval.tv_usec - start_timeval.tv_usec);
290 290
 
291
-    seconds = end_timeb.time -
292
-              start_timeb.time -
293
-              ( end_timeb.millitm < start_timeb.millitm ? 1 : 0);
291
+    seconds = end_timeval.tv_sec -
292
+              start_timeval.tv_sec -
293
+              ( end_timeval.tv_usec < start_timeval.tv_usec ? 1 : 0);
294 294
 
295 295
     if( seconds < 0)
296 296
     {
... ...
@@ -336,7 +332,7 @@ static int set_duration( struct dlg_cell* dialog)
336 336
 static int set_start_time( struct dlg_cell* dialog)
337 337
 {
338 338
     char buffer[ time_buffer_length];
339
-    struct timeb current_time = time_error;
339
+    struct timeval current_time;
340 340
     int buffer_length = -1;
341 341
     str start_time = empty_string;
342 342
 
... ...
@@ -346,7 +342,7 @@ static int set_start_time( struct dlg_cell* dialog)
346 346
         return -1;
347 347
     }
348 348
 
349
-    if( ftime( &current_time) < 0)
349
+    if( gettimeofday( &current_time, NULL) < 0)
350 350
     {
351 351
         LM_ERR( "failed to get current time!\n");
352 352
         return -1;
... ...
@@ -355,9 +351,9 @@ static int set_start_time( struct dlg_cell* dialog)
355 355
     buffer_length = snprintf( buffer,
356 356
                               time_buffer_length,
357 357
                               "%d%c%03d",
358
-                              (int)current_time.time,
358
+                              (int)current_time.tv_sec,
359 359
                               time_separator,
360
-                              (int)current_time.millitm);
360
+                              (int)current_time.tv_usec);
361 361
 
362 362
     if( buffer_length < 0)
363 363
     {
... ...
@@ -398,7 +394,7 @@ static int set_start_time( struct dlg_cell* dialog)
398 398
 static int set_end_time( struct dlg_cell* dialog)
399 399
 {
400 400
     char buffer[ time_buffer_length];
401
-    struct timeb current_time = time_error;
401
+    struct timeval current_time;
402 402
     int buffer_length = -1;
403 403
     str end_time = empty_string;
404 404
 
... ...
@@ -408,7 +404,7 @@ static int set_end_time( struct dlg_cell* dialog)
408 408
         return -1;
409 409
     }
410 410
 
411
-    if( ftime( &current_time) < 0)
411
+    if( gettimeofday( &current_time, NULL) < 0)
412 412
     {
413 413
         LM_ERR( "failed to set time!\n");
414 414
         return -1;
... ...
@@ -417,9 +413,9 @@ static int set_end_time( struct dlg_cell* dialog)
417 417
     buffer_length = snprintf( buffer,
418 418
                               time_buffer_length,
419 419
                               "%d%c%03d",
420
-                              (int)current_time.time,
420
+                              (int)current_time.tv_sec,
421 421
                               time_separator,
422
-                              (int)current_time.millitm);
422
+                              (int)current_time.tv_usec);
423 423
 
424 424
     if( buffer_length < 0)
425 425
     {