Add a "time_secs_to_str_buf()" routine, which takes seconds and
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 1 Aug 2001 08:27:00 +0000 (08:27 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Wed, 1 Aug 2001 08:27:00 +0000 (08:27 +0000)
fractions-of-a-second (the units of which are either milliseconds or
microseconds, specified by a Boolean argument), and formats it into a
"DD days, HH hours, MM minutes, SS seconds" using a buffer supplied to
it.  Have "time_secs_to_str()" and "time_msecs_to_str()" both use it.
Also, have it correctly handle the case of SS being > 0 but < 1 (which
"time_msecs_to_str()" didn't do).

Rename "rel_time_to_str()" to "rel_time_to_secs_str()", and add a
"rel_time_to_str()" routine that takes a "struct timeval" and hands its
seconds and microseconds values to "time_secs_to_str_buf()".  Use
"rel_time_to_secs_str()" to format FT_RELATIVE_TIME values for now; we
might want to use "rel_time_to_str()" for them, though, or make it an
option (either a user option, or a per-field option, using the field
that also holds BASE_ values).

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@3806 f5534014-38df-0310-8fa8-9805f1628bb7

epan/proto.c
epan/to_str.c
epan/to_str.h

index 35de4bde33f630eab9c64f7cf65538c220847b65..d086ed21a0c1779665258d82fc0449bf3a6f7631 100644 (file)
@@ -1,7 +1,7 @@
 /* proto.c
  * Routines for protocol tree
  *
- * $Id: proto.c,v 1.30 2001/07/13 00:55:53 guy Exp $
+ * $Id: proto.c,v 1.31 2001/08/01 08:27:00 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -1956,7 +1956,7 @@ proto_item_fill_label(field_info *fi, gchar *label_str)
                case FT_RELATIVE_TIME:
                        snprintf(label_str, ITEM_LABEL_LENGTH,
                                "%s: %s seconds", hfinfo->name,
-                               rel_time_to_str(fvalue_get(fi->value)));
+                               rel_time_to_secs_str(fvalue_get(fi->value)));
                        break;
 
                case FT_IPXNET:
@@ -2964,7 +2964,7 @@ proto_alloc_dfilter_string(field_info *finfo, guint8 *pd)
 
                case FT_RELATIVE_TIME:
                        value_str =
-                           rel_time_to_str((struct timeval *)fvalue_get(finfo->value));
+                           rel_time_to_secs_str((struct timeval *)fvalue_get(finfo->value));
                        dfilter_len = abbrev_len + strlen(value_str) + 4;
                        buf = g_malloc0(dfilter_len);
                        snprintf(buf, dfilter_len, "%s == %s",
index 26229c2be3d7ffbee60161d8345d1d2d13779fae..01c64fba66f9df3ea5bb9db663a1e1dd22a5553d 100644 (file)
@@ -1,12 +1,11 @@
-/* to_str.h
- * Routines  for utilities to convert various other types to strings.
+/* to_str.c
+ * Routines for utilities to convert various other types to strings.
  *
- * $Id: to_str.c,v 1.10 2001/07/15 19:14:02 guy Exp $
+ * $Id: to_str.c,v 1.11 2001/08/01 08:27:00 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -268,27 +267,27 @@ vines_addr_to_str(const guint8 *addrp)
 #define        PLURALIZE(n)    (((n) > 1) ? "s" : "")
 #define        COMMA(do_it)    ((do_it) ? ", " : "")
 
-gchar *
-time_secs_to_str(guint32 time)
+/*
+ * Maximum length of a string showing days/hours/minutes/seconds.
+ * (Does not include the terminating '\0'.)
+ */
+#define TIME_SECS_LEN  (8+1+4+2+2+5+2+2+7+2+2+7)
+
+/*
+ * Convert a value in seconds and fractions of a second to a string,
+ * giving time in days, hours, minutes, and seconds, and put the result
+ * into a buffer.
+ * "is_usecs" says that "frac" is microseconds if true and milliseconds
+ * if false.
+ */
+static void
+time_secs_to_str_buf(guint32 time, guint32 frac, gboolean is_usecs,
+                          gchar *buf)
 {
-  static gchar  str[3][8+1+4+2+2+5+2+2+7+2+2+7+1];
-  static gchar *cur, *p;
+  static gchar *p;
   int hours, mins, secs;
   int do_comma;
 
-  if (cur == &str[0][0]) {
-    cur = &str[1][0];
-  } else if (cur == &str[1][0]) {  
-    cur = &str[2][0];
-  } else {  
-    cur = &str[0][0];
-  }
-
-  if (time == 0) {
-    sprintf(cur, "0 time");
-    return cur;
-  }
-
   secs = time % 60;
   time /= 60;
   mins = time % 60;
@@ -296,7 +295,7 @@ time_secs_to_str(guint32 time)
   hours = time % 24;
   time /= 24;
 
-  p = cur;
+  p = buf;
   if (time != 0) {
     sprintf(p, "%u day%s", time, PLURALIZE(time));
     p += strlen(p);
@@ -315,19 +314,46 @@ time_secs_to_str(guint32 time)
     do_comma = 1;
   } else
     do_comma = 0;
-  if (secs != 0)
-    sprintf(p, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
+  if (secs != 0 || frac != 0) {
+    if (frac != 0) {
+      if (is_usecs)
+        sprintf(p, "%s%u.%06u seconds", COMMA(do_comma), secs, frac);
+      else
+        sprintf(p, "%s%u.%03u seconds", COMMA(do_comma), secs, frac);
+    } else
+      sprintf(p, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
+  }
+}
+
+gchar *
+time_secs_to_str(guint32 time)
+{
+  static gchar  str[3][TIME_SECS_LEN+1];
+  static gchar *cur;
+
+  if (cur == &str[0][0]) {
+    cur = &str[1][0];
+  } else if (cur == &str[1][0]) {  
+    cur = &str[2][0];
+  } else {  
+    cur = &str[0][0];
+  }
+
+  if (time == 0) {
+    sprintf(cur, "0 time");
+    return cur;
+  }
+
+  time_secs_to_str_buf(time, 0, FALSE, cur);
   return cur;
 }
 
 gchar *
 time_msecs_to_str(guint32 time)
 {
-  static gchar  str[3][8+1+4+2+2+5+2+2+7+2+2+11+1];
-  static gchar *cur, *p;
-  int hours, mins, secs;
+  static gchar  str[3][TIME_SECS_LEN+1+3+1];
+  static gchar *cur;
   int msecs;
-  int do_comma;
 
   if (cur == &str[0][0]) {
     cur = &str[1][0];
@@ -345,38 +371,7 @@ time_msecs_to_str(guint32 time)
   msecs = time % 1000;
   time /= 1000;
 
-  secs = time % 60;
-  time /= 60;
-  mins = time % 60;
-  time /= 60;
-  hours = time % 24;
-  time /= 24;
-
-  p = cur;
-  if (time != 0) {
-    sprintf(p, "%u day%s", time, PLURALIZE(time));
-    p += strlen(p);
-    do_comma = 1;
-  } else
-    do_comma = 0;
-  if (hours != 0) {
-    sprintf(p, "%s%u hour%s", COMMA(do_comma), hours, PLURALIZE(hours));
-    p += strlen(p);
-    do_comma = 1;
-  } else
-    do_comma = 0;
-  if (mins != 0) {
-    sprintf(p, "%s%u minute%s", COMMA(do_comma), mins, PLURALIZE(mins));
-    p += strlen(p);
-    do_comma = 1;
-  } else
-    do_comma = 0;
-  if (secs != 0) {
-    if (msecs != 0)
-      sprintf(p, "%s%u.%03u seconds", COMMA(do_comma), secs, msecs);
-    else
-      sprintf(p, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
-  }
+  time_secs_to_str_buf(time, msecs, FALSE, cur);
   return cur;
 }
 
@@ -426,8 +421,6 @@ abs_time_to_str(struct timeval *abs_time)
         return cur;
 }
 
-#define        REL_TIME_LEN    (1+10+1+6+1)
-
 void
 display_signed_time(gchar *buf, int buflen, gint32 sec, gint32 usec)
 {
@@ -446,11 +439,65 @@ display_signed_time(gchar *buf, int buflen, gint32 sec, gint32 usec)
        snprintf(buf, buflen, "%s%d.%06d", sign, sec, usec);
 }
 
+/*
+ * Display a relative time as days/hours/minutes/seconds.
+ */
 gchar *
 rel_time_to_str(struct timeval *rel_time)
+{
+       static gchar *cur;
+       static char str[3][1+TIME_SECS_LEN+1+6+1];
+       char *p;
+       char *sign;
+       guint32 time;
+       gint32 usec;
+
+       if (cur == &str[0][0]) {
+               cur = &str[1][0];
+       } else if (cur == &str[1][0]) {
+               cur = &str[2][0];
+       } else {
+               cur = &str[0][0];
+       }
+       p = cur;
+
+       /* If the microseconds part of the time stamp is negative,
+          print its absolute value and, if the seconds part isn't
+          (the seconds part should be zero in that case), stick
+          a "-" in front of the entire time stamp. */
+       sign = "";
+       time = rel_time->tv_sec;
+       usec = rel_time->tv_usec;
+       if (time == 0 && usec == 0) {
+               sprintf(cur, "0.000000 seconds");
+               return cur;
+       }
+       if (usec < 0) {
+               usec = -usec;
+               *p++ = '-';
+
+               /*
+                * We assume here that "rel_time->tv_sec" is negative
+                * or zero; if it's not, the time stamp is bogus,
+                * with a positive seconds and negative microseconds.
+                */
+               time = -rel_time->tv_sec;
+       }
+
+       time_secs_to_str_buf(time, usec, TRUE, p);
+       return cur;
+}
+
+#define REL_TIME_SECS_LEN      (1+10+1+6+1)
+
+/*
+ * Display a relative time as seconds.
+ */
+gchar *
+rel_time_to_secs_str(struct timeval *rel_time)
 {
         static gchar *cur;
-        static char str[3][REL_TIME_LEN];
+        static char str[3][REL_TIME_SECS_LEN];
 
         if (cur == &str[0][0]) {
                 cur = &str[1][0];
@@ -460,8 +507,8 @@ rel_time_to_str(struct timeval *rel_time)
                 cur = &str[0][0];
         }
 
-       display_signed_time(cur, REL_TIME_LEN, rel_time->tv_sec,
-           rel_time->tv_usec);
+        display_signed_time(cur, REL_TIME_SECS_LEN, rel_time->tv_sec,
+            rel_time->tv_usec);
         return cur;
 }
 
index c657d523d3a162de32fbba96143a9319c8a759f3..e706f9e60bd607adc2d6b8459cc67b9d0fb24ba8 100644 (file)
@@ -1,12 +1,11 @@
 /* to_str.h
  * Definitions for utilities to convert various other types to strings.
  *
- * $Id: to_str.h,v 1.3 2001/07/13 00:27:51 guy Exp $
+ * $Id: to_str.h,v 1.4 2001/08/01 08:27:00 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -56,6 +55,7 @@ gchar*     time_msecs_to_str(guint32);
 gchar*    abs_time_to_str(struct timeval*);
 void       display_signed_time(gchar *, int, gint32, gint32);
 gchar*    rel_time_to_str(struct timeval*);
+gchar*    rel_time_to_secs_str(struct timeval*);
 
 
 char * decode_bitfield_value(char *buf, guint32 val, guint32 mask, int width);