Imported Upstream version 2.6.1
[debian/amanda] / common-src / clock.c
index 840992f86378f45c85cf06960eee3667e799c718..33bcaba7882ccab0a50bcfdac4d26718230d6cb0 100644 (file)
@@ -25,7 +25,7 @@
  *                        University of Maryland at College Park
  */
 /*
- * $Id: clock.c,v 1.2.2.2 2002/03/31 21:01:33 jrjackson Exp $
+ * $Id: clock.c,v 1.7 2006/07/27 18:12:10 martinea Exp $
  *
  * timing functions
  */
 #include "clock.h"
 
 /* local functions */
-static struct timeval timesub P((struct timeval end, struct timeval start));
-static struct timeval timeadd P((struct timeval a, struct timeval b));
-
-times_t times_zero = {{0,0}};
 times_t start_time;
 static int clock_running = 0;
 
-#ifdef HAVE_TWO_ARG_GETTIMEOFDAY
-#  define amanda_gettimeofday(x, y) gettimeofday((x), (y))
-#else
-#  define amanda_gettimeofday(x, y) gettimeofday((x))
-#endif
-
-int clock_is_running()
+int
+clock_is_running(void)
 {
     return clock_running;
 }
 
-void startclock()
+void
+startclock(void)
 {
-#ifdef HAVE_TWO_ARG_GETTIMEOFDAY
-    struct timezone dontcare;
-#endif
-
     clock_running = 1;
-    amanda_gettimeofday(&start_time.r, &dontcare);
+    
+    g_get_current_time(&start_time);
 }
 
-times_t stopclock()
+times_t
+stopclock(void)
 {
-    times_t diff;
-    struct timeval end_time;
+    GTimeVal diff;
 
-#ifdef HAVE_TWO_ARG_GETTIMEOFDAY
-    struct timezone dontcare;
-#endif
+    diff = curclock();
 
-    if(!clock_running) {
-       fprintf(stderr,"stopclock botch\n");
-       exit(1);
-    }
-    amanda_gettimeofday(&end_time, &dontcare);
-    diff.r = timesub(end_time,start_time.r);
     clock_running = 0;
     return diff;
 }
 
-times_t curclock()
+times_t
+curclock(void)
 {
-    times_t diff;
-    struct timeval end_time;
-
-#ifdef HAVE_TWO_ARG_GETTIMEOFDAY
-    struct timezone dontcare;
-#endif
+    GTimeVal end_time;
 
     if(!clock_running) {
-       fprintf(stderr,"curclock botch\n");
+       g_fprintf(stderr,_("curclock botch\n"));
        exit(1);
     }
-    amanda_gettimeofday(&end_time, &dontcare);
-    diff.r = timesub(end_time,start_time.r);
-    return diff;
-}
 
-times_t timesadd(a,b)
-times_t a,b;
-{
-    times_t sum;
-
-    sum.r = timeadd(a.r,b.r);
-    return sum;
+    g_get_current_time(&end_time);
+    return timesub(end_time,start_time);
 }
 
-times_t timessub(a,b)
-times_t a,b;
-{
-    times_t dif;
-
-    dif.r = timesub(a.r,b.r);
-    return dif;
-}
-
-char *times_str(t)
-times_t t;
+char *
+walltime_str(
+    times_t    t)
 {
     static char str[10][NUM_STR_SIZE+10];
-    static int n = 0;
+    static size_t n = 0;
     char *s;
 
     /* tv_sec/tv_usec are longs on some systems */
-    ap_snprintf(str[n], sizeof(str[n]),
-               "rtime %d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
+    g_snprintf(str[n], SIZEOF(str[n]), "%lu.%03lu",
+            (unsigned long)t.tv_sec,
+            (unsigned long)t.tv_usec/1000);
     s = str[n++];
     n %= am_countof(str);
     return s;
 }
 
-char *walltime_str(t)
-times_t t;
-{
-    static char str[10][NUM_STR_SIZE+10];
-    static int n = 0;
-    char *s;
-
-    /* tv_sec/tv_usec are longs on some systems */
-    ap_snprintf(str[n], sizeof(str[n]),
-               "%d.%03d", (int)t.r.tv_sec, (int)t.r.tv_usec/1000);
-    s = str[n++];
-    n %= am_countof(str);
-    return s;
-}
-
-static struct timeval timesub(end,start)
-struct timeval end,start;
-{
-    struct timeval diff;
+GTimeVal timesub(GTimeVal end, GTimeVal start) {
+    GTimeVal diff;
 
     if(end.tv_usec < start.tv_usec) { /* borrow 1 sec */
-       end.tv_sec -= 1;
+       if (end.tv_sec > 0)
+           end.tv_sec -= 1;
        end.tv_usec += 1000000;
     }
     diff.tv_usec = end.tv_usec - start.tv_usec;
-    diff.tv_sec = end.tv_sec - start.tv_sec;
+
+    if (end.tv_sec > start.tv_sec)
+       diff.tv_sec = end.tv_sec - start.tv_sec;
+    else
+       diff.tv_sec = 0;
+
     return diff;
 }
 
-static struct timeval timeadd(a,b)
-struct timeval a,b;
-{
-    struct timeval sum;
+GTimeVal timeadd(GTimeVal a, GTimeVal b) {
+    GTimeVal sum;
 
     sum.tv_sec = a.tv_sec + b.tv_sec;
     sum.tv_usec = a.tv_usec + b.tv_usec;
@@ -175,3 +123,15 @@ struct timeval a,b;
     }
     return sum;
 }
+
+double g_timeval_to_double(GTimeVal v) {
+    return v.tv_sec + ((double)v.tv_usec) / G_USEC_PER_SEC;
+}
+
+void amanda_gettimeofday(struct timeval * timeval_time) {
+    GTimeVal gtimeval_time;
+
+    g_get_current_time(&gtimeval_time);
+    timeval_time->tv_sec = gtimeval_time.tv_sec;
+    timeval_time->tv_usec = gtimeval_time.tv_usec;
+}