Add a set of time stamp precision values in wsutil/nstime.h and use them.

Add an enum containing a set of symbols for time stamp precisions, where
the value of the symbol is the number of digits of precision after the
decimal point.

Replace to_str_time_res_t with the new enum, ws_tsprec_e.

Deefine the TS_PREC_FIXED_ entries in the ts_precision enum, and the
WTAP_TSPREC_ #defines that correspond to known time stamp precisions, to
have the same values as the corresponding ws_tsprec_e values. This means
that their values are also the number of digits of precision after the
decimal point.
This commit is contained in:
Guy Harris 2023-08-15 23:22:30 -07:00
parent 14b8bc66e8
commit 5d04013024
8 changed files with 73 additions and 61 deletions

View File

@ -1258,27 +1258,27 @@ set_time_seconds(const frame_data *fd, const nstime_t *ts, gchar *buf)
switch (tsprecision) {
case WTAP_TSPREC_SEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
(gint64) ts->secs, ts->nsecs / 1000000000, WS_TSPREC_SEC);
break;
case WTAP_TSPREC_DSEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
(gint64) ts->secs, ts->nsecs / 100000000, WS_TSPREC_100_MSEC);
break;
case WTAP_TSPREC_CSEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
(gint64) ts->secs, ts->nsecs / 10000000, WS_TSPREC_10_MSEC);
break;
case WTAP_TSPREC_MSEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
(gint64) ts->secs, ts->nsecs / 1000000, WS_TSPREC_MSEC);
break;
case WTAP_TSPREC_USEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000, TO_STR_TIME_RES_T_USECS);
(gint64) ts->secs, ts->nsecs / 1000, WS_TSPREC_USEC);
break;
case WTAP_TSPREC_NSEC:
display_signed_time(buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs, TO_STR_TIME_RES_T_NSECS);
(gint64) ts->secs, ts->nsecs, WS_TSPREC_NSEC);
break;
default:
ws_assert_not_reached();
@ -1716,27 +1716,27 @@ set_epoch_time(const frame_data *fd, gchar *buf)
switch (tsprecision) {
case WTAP_TSPREC_SEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000000, WS_TSPREC_SEC);
break;
case WTAP_TSPREC_DSEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
fd->abs_ts.secs, fd->abs_ts.nsecs / 100000000, WS_TSPREC_100_MSEC);
break;
case WTAP_TSPREC_CSEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
fd->abs_ts.secs, fd->abs_ts.nsecs / 10000000, WS_TSPREC_10_MSEC);
break;
case WTAP_TSPREC_MSEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000, WS_TSPREC_MSEC);
break;
case WTAP_TSPREC_USEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000, TO_STR_TIME_RES_T_USECS);
fd->abs_ts.secs, fd->abs_ts.nsecs / 1000, WS_TSPREC_USEC);
break;
case WTAP_TSPREC_NSEC:
display_epoch_time(buf, COL_MAX_LEN,
fd->abs_ts.secs, fd->abs_ts.nsecs, TO_STR_TIME_RES_T_NSECS);
fd->abs_ts.secs, fd->abs_ts.nsecs, WS_TSPREC_NSEC);
break;
default:
ws_assert_not_reached();
@ -1991,28 +1991,28 @@ col_set_time(column_info *cinfo, const gint el, const nstime_t *ts, const char *
switch (timestamp_get_precision()) {
case TS_PREC_FIXED_SEC:
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
(gint64) ts->secs, ts->nsecs / 1000000000, WS_TSPREC_SEC);
break;
case TS_PREC_FIXED_DSEC:
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
(gint64) ts->secs, ts->nsecs / 100000000, WS_TSPREC_100_MSEC);
break;
case TS_PREC_FIXED_CSEC:
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
(gint64) ts->secs, ts->nsecs / 10000000, WS_TSPREC_10_MSEC);
break;
case TS_PREC_FIXED_MSEC:
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
(gint64) ts->secs, ts->nsecs / 1000000, WS_TSPREC_MSEC);
break;
case TS_PREC_FIXED_USEC:
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs / 1000, TO_STR_TIME_RES_T_USECS);
(gint64) ts->secs, ts->nsecs / 1000, WS_TSPREC_USEC);
break;
case TS_PREC_FIXED_NSEC:
case TS_PREC_AUTO: /* default to maximum */
display_signed_time(col_item->col_buf, COL_MAX_LEN,
(gint64) ts->secs, ts->nsecs, TO_STR_TIME_RES_T_NSECS);
(gint64) ts->secs, ts->nsecs, WS_TSPREC_NSEC);
break;
default:
ws_assert_not_reached();

View File

@ -13,6 +13,8 @@
#include "ws_symbol_export.h"
#include <wsutil/nstime.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@ -40,15 +42,19 @@ typedef enum {
} ts_type;
typedef enum {
TS_PREC_AUTO,
TS_PREC_FIXED_SEC,
TS_PREC_FIXED_DSEC,
TS_PREC_FIXED_CSEC,
TS_PREC_FIXED_MSEC,
TS_PREC_FIXED_USEC,
TS_PREC_FIXED_NSEC,
/* Special value akin to TS_NOT_SET */
TS_PREC_NOT_SET
TS_PREC_AUTO = -1, /* Use what the capture file specifies */
TS_PREC_FIXED_SEC = WS_TSPREC_SEC,
TS_PREC_FIXED_DSEC = WS_TSPREC_100_MSEC,
TS_PREC_FIXED_CSEC = WS_TSPREC_10_MSEC,
TS_PREC_FIXED_MSEC = WS_TSPREC_MSEC,
TS_PREC_FIXED_USEC = WS_TSPREC_USEC,
TS_PREC_FIXED_NSEC = WS_TSPREC_NSEC,
/*
* Special value used for the command-line setting in Wireshark, to indicate
* that no value has been set from the command line.
*/
TS_PREC_NOT_SET = -2
} ts_precision;
typedef enum {

View File

@ -443,7 +443,7 @@ rel_time_to_secs_str(wmem_allocator_t *scope, const nstime_t *rel_time)
buf = (gchar *)wmem_alloc(scope, NSTIME_SECS_LEN);
display_signed_time(buf, NSTIME_SECS_LEN, (gint64) rel_time->secs,
rel_time->nsecs, TO_STR_TIME_RES_T_NSECS);
rel_time->nsecs, WS_TSPREC_NSEC);
return buf;
}
@ -455,7 +455,7 @@ abs_time_to_unix_str(wmem_allocator_t *scope, const nstime_t *rel_time)
buf = (gchar *)wmem_alloc(scope, NSTIME_SECS_LEN);
display_epoch_time(buf, NSTIME_SECS_LEN, (gint64) rel_time->secs,
rel_time->nsecs, TO_STR_TIME_RES_T_NSECS);
rel_time->nsecs, WS_TSPREC_NSEC);
return buf;
}

View File

@ -320,12 +320,12 @@ extern "C" {
/* timestamp precision (currently only these values are supported) */
#define WTAP_TSPREC_UNKNOWN -2
#define WTAP_TSPREC_PER_PACKET -1 /* as a per-file value, means per-packet */
#define WTAP_TSPREC_SEC 0
#define WTAP_TSPREC_DSEC 1
#define WTAP_TSPREC_CSEC 2
#define WTAP_TSPREC_MSEC 3
#define WTAP_TSPREC_USEC 6
#define WTAP_TSPREC_NSEC 9
#define WTAP_TSPREC_SEC WS_TSPREC_SEC
#define WTAP_TSPREC_DSEC WS_TSPREC_100_MSEC
#define WTAP_TSPREC_CSEC WS_TSPREC_10_MSEC
#define WTAP_TSPREC_MSEC WS_TSPREC_MSEC
#define WTAP_TSPREC_USEC WS_TSPREC_USEC
#define WTAP_TSPREC_NSEC WS_TSPREC_NSEC
/* if you add to the above, update wtap_tsprec_string() */
/*

View File

@ -632,7 +632,7 @@ size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_t *nstime)
void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime)
{
display_signed_time(buf, buf_size, (gint64) nstime->secs,
nstime->nsecs, TO_STR_TIME_RES_T_NSECS);
nstime->nsecs, WS_TSPREC_NSEC);
}
/*

View File

@ -148,6 +148,20 @@ WS_DLL_PUBLIC size_t nstime_to_iso8601(char *buf, size_t buf_size, const nstime_
WS_DLL_PUBLIC void nstime_to_unix(char *buf, size_t buf_size, const nstime_t *nstime);
/*
* Timestamp precision values.
*
* The value is the number of digits of precision after the integral part.
*/
typedef enum {
WS_TSPREC_SEC = 0,
WS_TSPREC_100_MSEC = 1,
WS_TSPREC_10_MSEC = 2,
WS_TSPREC_MSEC = 3,
WS_TSPREC_USEC = 6,
WS_TSPREC_NSEC = 9
} ws_tsprec_e;
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -642,7 +642,7 @@ eui64_to_str(wmem_allocator_t *scope, const guint64 ad) {
void
display_epoch_time(gchar *buf, size_t buflen, const time_t sec, gint32 frac,
const to_str_time_res_t units)
const ws_tsprec_e units)
{
double elapsed_secs;
@ -667,27 +667,27 @@ display_epoch_time(gchar *buf, size_t buflen, const time_t sec, gint32 frac,
}
switch (units) {
case TO_STR_TIME_RES_T_SECS:
case WS_TSPREC_SEC:
snprintf(buf, buflen, "%0.0f", elapsed_secs);
break;
case TO_STR_TIME_RES_T_DSECS:
case WS_TSPREC_100_MSEC:
snprintf(buf, buflen, "%0.0f.%01d", elapsed_secs, frac);
break;
case TO_STR_TIME_RES_T_CSECS:
case WS_TSPREC_10_MSEC:
snprintf(buf, buflen, "%0.0f.%02d", elapsed_secs, frac);
break;
case TO_STR_TIME_RES_T_MSECS:
case WS_TSPREC_MSEC:
snprintf(buf, buflen, "%0.0f.%03d", elapsed_secs, frac);
break;
case TO_STR_TIME_RES_T_USECS:
case WS_TSPREC_USEC:
snprintf(buf, buflen, "%0.0f.%06d", elapsed_secs, frac);
break;
case TO_STR_TIME_RES_T_NSECS:
case WS_TSPREC_NSEC:
snprintf(buf, buflen, "%0.0f.%09d", elapsed_secs, frac);
break;
}
@ -704,7 +704,7 @@ display_epoch_time(gchar *buf, size_t buflen, const time_t sec, gint32 frac,
void
display_signed_time(gchar *buf, size_t buflen, const gint64 sec, gint32 frac,
const to_str_time_res_t units)
const ws_tsprec_e units)
{
/* this buffer is not NUL terminated */
gint8 num_buf[CHARS_64_BIT_SIGNED];
@ -736,29 +736,29 @@ display_signed_time(gchar *buf, size_t buflen, const gint64 sec, gint32 frac,
buflen -= num_len;
switch (units) {
case TO_STR_TIME_RES_T_SECS:
case WS_TSPREC_SEC:
default:
/* no fraction */
num_ptr = NULL;
break;
case TO_STR_TIME_RES_T_DSECS:
case WS_TSPREC_100_MSEC:
num_ptr = uint_to_str_back_len(num_end, frac, 1);
break;
case TO_STR_TIME_RES_T_CSECS:
case WS_TSPREC_10_MSEC:
num_ptr = uint_to_str_back_len(num_end, frac, 2);
break;
case TO_STR_TIME_RES_T_MSECS:
case WS_TSPREC_MSEC:
num_ptr = uint_to_str_back_len(num_end, frac, 3);
break;
case TO_STR_TIME_RES_T_USECS:
case WS_TSPREC_USEC:
num_ptr = uint_to_str_back_len(num_end, frac, 6);
break;
case TO_STR_TIME_RES_T_NSECS:
case WS_TSPREC_NSEC:
num_ptr = uint_to_str_back_len(num_end, frac, 9);
break;
}

View File

@ -16,15 +16,7 @@
#include <wsutil/wmem/wmem.h>
#include <wsutil/inet_ipv6.h>
typedef enum {
TO_STR_TIME_RES_T_SECS, /* seconds */
TO_STR_TIME_RES_T_DSECS, /* deciseconds */
TO_STR_TIME_RES_T_CSECS, /* centiseconds */
TO_STR_TIME_RES_T_MSECS, /* milliseconds */
TO_STR_TIME_RES_T_USECS, /* microseconds */
TO_STR_TIME_RES_T_NSECS /* nanoseconds */
} to_str_time_res_t;
#include <wsutil/nstime.h>
#ifdef __cplusplus
extern "C" {
@ -307,9 +299,9 @@ WS_DLL_PUBLIC gchar *ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32
WS_DLL_PUBLIC gchar *eui64_to_str(wmem_allocator_t *scope, const guint64 ad);
WS_DLL_PUBLIC void display_epoch_time(gchar *, size_t, const time_t, gint32, const to_str_time_res_t);
WS_DLL_PUBLIC void display_epoch_time(gchar *, size_t, const time_t, gint32, const ws_tsprec_e);
WS_DLL_PUBLIC void display_signed_time(gchar *, size_t, const gint64, gint32, const to_str_time_res_t);
WS_DLL_PUBLIC void display_signed_time(gchar *, size_t, const gint64, gint32, const ws_tsprec_e);
#ifdef __cplusplus
}