Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
/* to_str.c
|
|
|
|
* Routines for utilities to convert various other types to strings.
|
2001-04-01 02:47:56 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-04-01 02:47:56 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2018-02-08 16:59:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2001-04-01 02:47:56 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
2001-04-01 02:47:56 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-02-27 21:41:12 +00:00
|
|
|
#include <string.h>
|
2001-04-01 02:47:56 +00:00
|
|
|
#include <time.h>
|
2010-02-27 21:41:12 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2021-07-26 00:31:17 +00:00
|
|
|
#include <epan/wmem_scopes.h>
|
2011-12-14 02:46:16 +00:00
|
|
|
#include "proto.h"
|
2010-02-27 21:41:12 +00:00
|
|
|
#include "to_str.h"
|
2013-03-01 23:53:11 +00:00
|
|
|
#include "strutil.h"
|
2015-02-09 01:36:03 +00:00
|
|
|
#include <wsutil/pint.h>
|
2018-08-26 11:48:08 +00:00
|
|
|
#include <wsutil/utf8_entities.h>
|
2001-04-01 02:47:56 +00:00
|
|
|
|
2007-01-31 23:26:29 +00:00
|
|
|
/*
|
|
|
|
* If a user _does_ pass in a too-small buffer, this is probably
|
|
|
|
* going to be too long to fit. However, even a partial string
|
|
|
|
* starting with "[Buf" should provide enough of a clue to be
|
|
|
|
* useful.
|
|
|
|
*/
|
|
|
|
#define BUF_TOO_SMALL_ERR "[Buffer too small]"
|
|
|
|
|
2013-06-04 18:13:09 +00:00
|
|
|
static const char mon_names[12][4] = {
|
2001-04-01 02:47:56 +00:00
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec"
|
|
|
|
};
|
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
static const gchar *
|
|
|
|
get_zonename(struct tm *tmp)
|
|
|
|
{
|
2017-01-01 02:45:00 +00:00
|
|
|
#if defined(_WIN32)
|
2020-09-02 06:52:35 +00:00
|
|
|
/*
|
|
|
|
* The strings in _tzname[] are encoded using the code page
|
|
|
|
* for the current C-language locale.
|
|
|
|
*
|
|
|
|
* On Windows, all Wireshark programs set that code page
|
|
|
|
* to the UTF-8 code page by calling
|
|
|
|
*
|
|
|
|
* setlocale(LC_ALL, ".UTF-8");
|
|
|
|
*
|
|
|
|
* so the strings in _tzname[] are UTF-8 strings, and we can
|
|
|
|
* just return them.
|
|
|
|
*
|
|
|
|
* (Note: the above does *not* mean we've set any code pages
|
|
|
|
* *other* than the one used by the Visual Studio C runtime
|
|
|
|
* to UTF-8, so don't assume, for example, that the "ANSI"
|
|
|
|
* versions of Windows APIs will take UTF-8 strings, or that
|
|
|
|
* non-UTF-16 output to the console will be treated as UTF-8.
|
|
|
|
* Setting those other code pages can cause problems, especially
|
|
|
|
* on pre-Windows 10 or older Windows 10 releases.)
|
|
|
|
*/
|
2020-09-02 19:35:14 +00:00
|
|
|
return _tzname[tmp->tm_isdst];
|
2017-01-01 02:45:00 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* UN*X.
|
|
|
|
*
|
|
|
|
* If we have tm_zone in struct tm, use that.
|
|
|
|
* Otherwise, if we have tzname[], use it, otherwise just
|
|
|
|
* say "we don't know.
|
|
|
|
*/
|
|
|
|
# if defined(HAVE_STRUCT_TM_TM_ZONE)
|
|
|
|
return tmp->tm_zone;
|
|
|
|
# else /* HAVE_STRUCT_TM_TM_ZONE */
|
|
|
|
if ((tmp->tm_isdst != 0) && (tmp->tm_isdst != 1)) {
|
|
|
|
return "???";
|
|
|
|
}
|
2017-01-01 05:45:51 +00:00
|
|
|
# if defined(HAVE_TZNAME)
|
2017-01-01 02:45:00 +00:00
|
|
|
return tzname[tmp->tm_isdst];
|
|
|
|
# else
|
2010-12-01 01:31:49 +00:00
|
|
|
return tmp->tm_isdst ? "?DT" : "?ST";
|
2017-01-01 02:45:00 +00:00
|
|
|
# endif /* HAVE_TZNAME */
|
|
|
|
# endif /* HAVE_STRUCT_TM_TM_ZONE */
|
|
|
|
#endif /* _WIN32 */
|
2010-12-01 01:31:49 +00:00
|
|
|
}
|
|
|
|
|
2001-04-01 02:47:56 +00:00
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
abs_time_to_str(wmem_allocator_t *scope, const nstime_t *abs_time, const absolute_time_display_e fmt,
|
2013-12-19 15:27:44 +00:00
|
|
|
gboolean show_zone)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
struct tm *tmp = NULL;
|
|
|
|
const char *zonename = "???";
|
|
|
|
gchar *buf = NULL;
|
2001-04-01 02:47:56 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
switch (fmt) {
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
2019-08-28 19:04:32 +00:00
|
|
|
case ABSOLUTE_TIME_NTP_UTC:
|
2014-01-26 12:56:32 +00:00
|
|
|
tmp = gmtime(&abs_time->secs);
|
|
|
|
zonename = "UTC";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
tmp = localtime(&abs_time->secs);
|
|
|
|
if (tmp) {
|
|
|
|
zonename = get_zonename(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-12-19 15:27:44 +00:00
|
|
|
if (tmp) {
|
|
|
|
switch (fmt) {
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d.%09ld %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d.%09ld",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs);
|
|
|
|
}
|
|
|
|
break;
|
2019-08-28 19:04:32 +00:00
|
|
|
case ABSOLUTE_TIME_NTP_UTC:
|
2020-08-11 09:14:28 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
2019-08-28 19:04:32 +00:00
|
|
|
if ((abs_time->secs == 0) && (abs_time->nsecs == 0)) {
|
2020-08-12 06:51:46 +00:00
|
|
|
if (show_zone) {
|
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"(0)%s %2d, %d %02d:%02d:%02d.%09ld %s",
|
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs,
|
|
|
|
zonename);
|
|
|
|
} else {
|
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"(0)%s %2d, %d %02d:%02d:%02d.%09ld",
|
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs);
|
|
|
|
}
|
2019-08-28 19:04:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-19 15:27:44 +00:00
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d.%09ld %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d.%09ld",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
(long)abs_time->nsecs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup(scope, "Not representable");
|
2013-12-19 15:27:44 +00:00
|
|
|
return buf;
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
abs_time_secs_to_str(wmem_allocator_t *scope, const time_t abs_time, const absolute_time_display_e fmt,
|
2013-12-19 15:27:44 +00:00
|
|
|
gboolean show_zone)
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
struct tm *tmp = NULL;
|
|
|
|
const char *zonename = "???";
|
|
|
|
gchar *buf = NULL;
|
Add a "abs_time_secs_to_str()" routine that takes a UNIX time-since-the-
epoch-in-seconds value and converts it to a string.
Use that routine in the RADIUS dissector, rather than using "ctime()"
and "tzname[]" - "tzname[]" strings might contain non-ASCII characters,
which currently give the GTK+ 1.3[.x] used on Windows, and also, I
think, GTK+ 2.x, heartburn, as they expect UTF-8, not, for example, ISO
8859/1.
Fix the string length in "abs_time_to_str()".
svn path=/trunk/; revision=7124
2003-02-11 19:42:38 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
switch (fmt) {
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
2019-08-28 19:04:32 +00:00
|
|
|
case ABSOLUTE_TIME_NTP_UTC:
|
2014-01-26 12:56:32 +00:00
|
|
|
tmp = gmtime(&abs_time);
|
|
|
|
zonename = "UTC";
|
|
|
|
break;
|
2013-12-19 15:27:44 +00:00
|
|
|
|
2014-01-26 12:56:32 +00:00
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
tmp = localtime(&abs_time);
|
|
|
|
if (tmp) {
|
|
|
|
zonename = get_zonename(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-12-19 15:27:44 +00:00
|
|
|
if (tmp) {
|
|
|
|
switch (fmt) {
|
|
|
|
|
|
|
|
case ABSOLUTE_TIME_DOY_UTC:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%04d/%03d:%02d:%02d:%02d",
|
2013-12-19 15:27:44 +00:00
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_yday + 1,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-08-28 19:04:32 +00:00
|
|
|
case ABSOLUTE_TIME_NTP_UTC:
|
|
|
|
if (abs_time == 0) {
|
|
|
|
buf = wmem_strdup(scope, "NULL");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2013-12-19 15:27:44 +00:00
|
|
|
case ABSOLUTE_TIME_UTC:
|
|
|
|
case ABSOLUTE_TIME_LOCAL:
|
|
|
|
if (show_zone) {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d %s",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec,
|
|
|
|
zonename);
|
|
|
|
} else {
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup_printf(scope,
|
|
|
|
"%s %2d, %d %02d:%02d:%02d",
|
2013-12-19 15:27:44 +00:00
|
|
|
mon_names[tmp->tm_mon],
|
|
|
|
tmp->tm_mday,
|
|
|
|
tmp->tm_year + 1900,
|
|
|
|
tmp->tm_hour,
|
|
|
|
tmp->tm_min,
|
|
|
|
tmp->tm_sec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2014-06-16 06:42:14 +00:00
|
|
|
buf = wmem_strdup(scope, "Not representable");
|
2013-12-19 15:27:44 +00:00
|
|
|
return buf;
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
void
|
|
|
|
display_epoch_time(gchar *buf, int buflen, const time_t sec, gint32 frac,
|
|
|
|
const to_str_time_res_t units)
|
|
|
|
{
|
|
|
|
double elapsed_secs;
|
|
|
|
|
|
|
|
elapsed_secs = difftime(sec,(time_t)0);
|
|
|
|
|
|
|
|
/* This code copied from display_signed_time; keep it in case anyone
|
|
|
|
is looking at captures from before 1970 (???).
|
|
|
|
If the fractional 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. */
|
|
|
|
if (frac < 0) {
|
|
|
|
frac = -frac;
|
|
|
|
if (elapsed_secs >= 0) {
|
|
|
|
if (buflen < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[0] = '-';
|
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (units) {
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_SECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f", elapsed_secs);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_DSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%01d", elapsed_secs, frac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_CSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%02d", elapsed_secs, frac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_MSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%03d", elapsed_secs, frac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_USECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%06d", elapsed_secs, frac);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TO_STR_TIME_RES_T_NSECS:
|
|
|
|
g_snprintf(buf, buflen, "%0.0f.%09d", elapsed_secs, frac);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 08:27:01 +00:00
|
|
|
/*
|
|
|
|
* Number of characters required by a 64-bit signed number.
|
|
|
|
*/
|
|
|
|
#define CHARS_64_BIT_SIGNED 20 /* sign plus 19 digits */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of characters required by a fractional part, in nanoseconds */
|
|
|
|
#define CHARS_NANOSECONDS 10 /* .000000001 */
|
|
|
|
|
2001-04-01 02:47:56 +00:00
|
|
|
void
|
2020-10-18 08:27:01 +00:00
|
|
|
display_signed_time(gchar *buf, int buflen, const gint64 sec, gint32 frac,
|
2013-12-19 15:27:44 +00:00
|
|
|
const to_str_time_res_t units)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2014-04-30 19:39:54 +00:00
|
|
|
/* this buffer is not NUL terminated */
|
2020-10-18 08:27:01 +00:00
|
|
|
gint8 num_buf[CHARS_64_BIT_SIGNED];
|
|
|
|
gint8 *num_end = &num_buf[CHARS_64_BIT_SIGNED];
|
2014-04-30 19:39:54 +00:00
|
|
|
gint8 *num_ptr;
|
|
|
|
int num_len;
|
|
|
|
|
|
|
|
if (buflen < 1)
|
|
|
|
return;
|
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
/* If the fractional part of the time stamp is negative,
|
2001-04-01 02:47:56 +00:00
|
|
|
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. */
|
2001-09-14 07:10:13 +00:00
|
|
|
if (frac < 0) {
|
|
|
|
frac = -frac;
|
2009-07-17 15:59:24 +00:00
|
|
|
if (sec >= 0) {
|
2009-07-20 12:40:55 +00:00
|
|
|
buf[0] = '-';
|
2009-07-17 15:59:24 +00:00
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
}
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2020-10-18 08:27:01 +00:00
|
|
|
num_ptr = int64_to_str_back(num_end, sec);
|
2014-04-30 19:39:54 +00:00
|
|
|
|
|
|
|
num_len = MIN((int) (num_end - num_ptr), buflen);
|
|
|
|
memcpy(buf, num_ptr, num_len);
|
|
|
|
buf += num_len;
|
|
|
|
buflen -= num_len;
|
|
|
|
|
|
|
|
switch (units) {
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_SECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
default:
|
|
|
|
/* no fraction */
|
|
|
|
num_ptr = NULL;
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_DSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 1);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_CSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 2);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2005-08-25 21:29:54 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_MSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 3);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_USECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 6);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
case TO_STR_TIME_RES_T_NSECS:
|
2014-04-30 19:39:54 +00:00
|
|
|
num_ptr = uint_to_str_back_len(num_end, frac, 9);
|
2013-12-19 15:27:44 +00:00
|
|
|
break;
|
2001-09-14 07:10:13 +00:00
|
|
|
}
|
2014-04-30 19:39:54 +00:00
|
|
|
|
|
|
|
if (num_ptr != NULL)
|
|
|
|
{
|
|
|
|
*(--num_ptr) = '.';
|
|
|
|
|
|
|
|
num_len = MIN((int) (num_end - num_ptr), buflen);
|
|
|
|
memcpy(buf, num_ptr, num_len);
|
|
|
|
buf += num_len;
|
|
|
|
buflen -= num_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need to NUL terminate, we know that buffer had at least 1 byte */
|
|
|
|
if (buflen == 0)
|
|
|
|
buf--;
|
|
|
|
*buf = '\0';
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
#define PLURALIZE(n) (((n) > 1) ? "s" : "")
|
|
|
|
#define COMMA(do_it) ((do_it) ? ", " : "")
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum length of a string showing days/hours/minutes/seconds.
|
|
|
|
* (Does not include the terminating '\0'.)
|
|
|
|
* Includes space for a '-' sign for any negative components.
|
|
|
|
* -12345 days, 12 hours, 12 minutes, 12.123 seconds
|
|
|
|
*/
|
|
|
|
#define TIME_SECS_LEN (10+1+4+2+2+5+2+2+7+2+2+7+4)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert an unsigned 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_nsecs" says that "frac" is nanoseconds if true and milliseconds
|
|
|
|
* if false.
|
|
|
|
*/
|
|
|
|
static void
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str_buf(guint32 time_val, const guint32 frac,
|
2016-05-09 20:21:44 +00:00
|
|
|
const gboolean is_nsecs, wmem_strbuf_t *buf)
|
2006-12-05 03:19:51 +00:00
|
|
|
{
|
2016-05-09 20:21:44 +00:00
|
|
|
int hours, mins, secs;
|
|
|
|
gboolean do_comma = FALSE;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
secs = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
mins = time_val % 60;
|
|
|
|
time_val /= 60;
|
|
|
|
hours = time_val % 24;
|
|
|
|
time_val /= 24;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
if (time_val != 0) {
|
|
|
|
wmem_strbuf_append_printf(buf, "%u day%s", time_val, PLURALIZE(time_val));
|
|
|
|
do_comma = TRUE;
|
2006-12-05 03:19:51 +00:00
|
|
|
}
|
2016-05-09 20:21:44 +00:00
|
|
|
if (hours != 0) {
|
|
|
|
wmem_strbuf_append_printf(buf, "%s%u hour%s", COMMA(do_comma), hours, PLURALIZE(hours));
|
|
|
|
do_comma = TRUE;
|
|
|
|
}
|
|
|
|
if (mins != 0) {
|
|
|
|
wmem_strbuf_append_printf(buf, "%s%u minute%s", COMMA(do_comma), mins, PLURALIZE(mins));
|
|
|
|
do_comma = TRUE;
|
|
|
|
}
|
|
|
|
if (secs != 0 || frac != 0) {
|
|
|
|
if (frac != 0) {
|
|
|
|
if (is_nsecs)
|
|
|
|
wmem_strbuf_append_printf(buf, "%s%u.%09u seconds", COMMA(do_comma), secs, frac);
|
|
|
|
else
|
|
|
|
wmem_strbuf_append_printf(buf, "%s%u.%03u seconds", COMMA(do_comma), secs, frac);
|
|
|
|
} else
|
|
|
|
wmem_strbuf_append_printf(buf, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
|
|
|
|
}
|
|
|
|
}
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
gchar *
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str(wmem_allocator_t *scope, const guint32 time_val)
|
2016-05-09 20:21:44 +00:00
|
|
|
{
|
|
|
|
wmem_strbuf_t *buf;
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
if (time_val == 0) {
|
|
|
|
return wmem_strdup(scope, "0 seconds");
|
|
|
|
}
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1, TIME_SECS_LEN+1);
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str_buf(time_val, 0, FALSE, buf);
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
return wmem_strbuf_finalize(buf);
|
|
|
|
}
|
2006-12-05 03:19:51 +00:00
|
|
|
|
2016-05-09 20:21:44 +00:00
|
|
|
/*
|
|
|
|
* Convert a signed 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_nsecs" says that "frac" is nanoseconds if true and milliseconds
|
|
|
|
* if false.
|
|
|
|
*/
|
|
|
|
static void
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_secs_to_str_buf(gint32 time_val, const guint32 frac,
|
|
|
|
const gboolean is_nsecs, wmem_strbuf_t *buf)
|
2016-05-09 20:21:44 +00:00
|
|
|
{
|
|
|
|
if(time_val < 0){
|
2016-05-09 22:20:08 +00:00
|
|
|
wmem_strbuf_append_printf(buf, "-");
|
|
|
|
if(time_val == G_MININT32) {
|
|
|
|
/*
|
|
|
|
* You can't fit time_val's absolute value into
|
|
|
|
* a 32-bit signed integer. Just directly
|
|
|
|
* pass G_MAXUINT32, which is its absolute
|
2016-05-09 22:54:27 +00:00
|
|
|
* value, directly to unsigned_time_secs_to_str_buf().
|
2016-05-09 22:20:08 +00:00
|
|
|
*
|
|
|
|
* (XXX - does ISO C guarantee that -(-2^n),
|
|
|
|
* when calculated and cast to an n-bit unsigned
|
|
|
|
* integer type, will have the value 2^n?)
|
|
|
|
*/
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str_buf(G_MAXUINT32, frac,
|
2016-05-09 22:20:08 +00:00
|
|
|
is_nsecs, buf);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We now know -secs will fit into a guint32;
|
|
|
|
* negate it and pass that to
|
2016-05-09 22:54:27 +00:00
|
|
|
* unsigned_time_secs_to_str_buf().
|
2016-05-09 22:20:08 +00:00
|
|
|
*/
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str_buf(-time_val, frac,
|
2016-05-09 22:20:08 +00:00
|
|
|
is_nsecs, buf);
|
|
|
|
}
|
|
|
|
} else
|
2016-05-09 22:54:27 +00:00
|
|
|
unsigned_time_secs_to_str_buf(time_val, frac, is_nsecs, buf);
|
2016-05-09 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_secs_to_str(wmem_allocator_t *scope, const gint32 time_val)
|
2016-05-09 20:21:44 +00:00
|
|
|
{
|
|
|
|
wmem_strbuf_t *buf;
|
|
|
|
|
|
|
|
if (time_val == 0) {
|
|
|
|
return wmem_strdup(scope, "0 seconds");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1, TIME_SECS_LEN+1);
|
|
|
|
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_secs_to_str_buf(time_val, 0, FALSE, buf);
|
2016-05-09 20:21:44 +00:00
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a signed value in milliseconds to a string, giving time in days,
|
|
|
|
* hours, minutes, and seconds, and put the result into a buffer.
|
|
|
|
*/
|
|
|
|
gchar *
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_msecs_to_str(wmem_allocator_t *scope, gint32 time_val)
|
2016-05-09 20:21:44 +00:00
|
|
|
{
|
|
|
|
wmem_strbuf_t *buf;
|
|
|
|
int msecs;
|
|
|
|
|
|
|
|
if (time_val == 0) {
|
|
|
|
return wmem_strdup(scope, "0 seconds");
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = wmem_strbuf_sized_new(scope, TIME_SECS_LEN+1+3+1, TIME_SECS_LEN+1+3+1);
|
|
|
|
|
|
|
|
if (time_val<0) {
|
|
|
|
/* oops we got passed a negative time */
|
|
|
|
time_val= -time_val;
|
|
|
|
msecs = time_val % 1000;
|
|
|
|
time_val /= 1000;
|
|
|
|
time_val= -time_val;
|
|
|
|
} else {
|
|
|
|
msecs = time_val % 1000;
|
|
|
|
time_val /= 1000;
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_secs_to_str_buf(time_val, msecs, FALSE, buf);
|
2016-05-09 20:21:44 +00:00
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
2006-12-05 03:19:51 +00:00
|
|
|
}
|
|
|
|
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
/*
|
|
|
|
* Display a relative time as days/hours/minutes/seconds.
|
|
|
|
*/
|
2001-04-01 02:47:56 +00:00
|
|
|
gchar *
|
2014-06-16 06:42:14 +00:00
|
|
|
rel_time_to_str(wmem_allocator_t *scope, const nstime_t *rel_time)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
{
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_t *buf;
|
2010-01-28 18:45:46 +00:00
|
|
|
gint32 time_val;
|
2001-09-14 07:10:13 +00:00
|
|
|
gint32 nsec;
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
/* If the nanoseconds part of the time stamp is negative,
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
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. */
|
2010-01-28 18:45:46 +00:00
|
|
|
time_val = (gint) rel_time->secs;
|
2001-09-14 07:10:13 +00:00
|
|
|
nsec = rel_time->nsecs;
|
2010-01-28 18:45:46 +00:00
|
|
|
if (time_val == 0 && nsec == 0) {
|
2014-06-16 06:42:14 +00:00
|
|
|
return wmem_strdup(scope, "0.000000000 seconds");
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
2014-06-16 06:42:14 +00:00
|
|
|
|
|
|
|
buf = wmem_strbuf_sized_new(scope, 1+TIME_SECS_LEN+1+6+1, 1+TIME_SECS_LEN+1+6+1);
|
|
|
|
|
2001-09-14 07:10:13 +00:00
|
|
|
if (nsec < 0) {
|
|
|
|
nsec = -nsec;
|
2014-06-16 06:42:14 +00:00
|
|
|
wmem_strbuf_append_c(buf, '-');
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
|
|
|
/*
|
2001-09-14 07:10:13 +00:00
|
|
|
* We assume here that "rel_time->secs" is negative
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
* or zero; if it's not, the time stamp is bogus,
|
|
|
|
* with a positive seconds and negative microseconds.
|
|
|
|
*/
|
2010-01-28 18:45:46 +00:00
|
|
|
time_val = (gint) -rel_time->secs;
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 22:54:27 +00:00
|
|
|
signed_time_secs_to_str_buf(time_val, nsec, TRUE, buf);
|
2014-06-16 06:42:14 +00:00
|
|
|
|
|
|
|
return wmem_strbuf_finalize(buf);
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 08:27:01 +00:00
|
|
|
/* Includes terminating '\0' */
|
|
|
|
#define REL_TIME_SECS_LEN (CHARS_64_BIT_SIGNED+CHARS_NANOSECONDS+1)
|
Add a "time_secs_to_str_buf()" routine, which takes seconds and
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).
svn path=/trunk/; revision=3806
2001-08-01 08:27:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a relative time as seconds.
|
|
|
|
*/
|
|
|
|
gchar *
|
2014-06-16 17:54:20 +00:00
|
|
|
rel_time_to_secs_str(wmem_allocator_t *scope, const nstime_t *rel_time)
|
2001-04-01 02:47:56 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
gchar *buf;
|
2005-07-22 08:27:20 +00:00
|
|
|
|
2014-06-16 17:54:20 +00:00
|
|
|
buf=(gchar *)wmem_alloc(scope, REL_TIME_SECS_LEN);
|
2005-07-22 08:27:20 +00:00
|
|
|
|
2020-10-18 08:27:01 +00:00
|
|
|
display_signed_time(buf, REL_TIME_SECS_LEN, (gint64) rel_time->secs,
|
2013-12-19 15:27:44 +00:00
|
|
|
rel_time->nsecs, TO_STR_TIME_RES_T_NSECS);
|
|
|
|
return buf;
|
2001-04-01 02:47:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
/*
|
|
|
|
* Generates a string representing the bits in a bitfield at "bit_offset" from an 8 bit boundary
|
|
|
|
* with the length in bits of no_of_bits based on value.
|
|
|
|
* Ex: ..xx x...
|
|
|
|
*/
|
2003-06-23 09:15:08 +00:00
|
|
|
|
2009-09-06 15:06:41 +00:00
|
|
|
char *
|
2012-04-04 21:54:22 +00:00
|
|
|
decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value)
|
2002-12-09 21:34:58 +00:00
|
|
|
{
|
2020-11-06 01:37:13 +00:00
|
|
|
guint64 mask;
|
2009-09-06 15:06:41 +00:00
|
|
|
char *str;
|
2012-12-09 23:12:59 +00:00
|
|
|
int bit, str_p = 0;
|
2009-09-06 15:06:41 +00:00
|
|
|
int i;
|
2020-11-06 01:37:13 +00:00
|
|
|
int max_bits = MIN(64, no_of_bits);
|
2002-12-08 02:32:36 +00:00
|
|
|
|
2020-11-06 01:37:13 +00:00
|
|
|
mask = G_GUINT64_CONSTANT(1) << (max_bits-1);
|
2003-01-21 05:04:07 +00:00
|
|
|
|
2012-12-09 23:12:59 +00:00
|
|
|
/* Prepare the string, 256 pos for the bits and zero termination, + 64 for the spaces */
|
2015-01-17 21:55:46 +00:00
|
|
|
str=(char *)wmem_alloc0(wmem_packet_scope(), 256+64);
|
2009-09-06 15:06:41 +00:00
|
|
|
for(bit=0;bit<((int)(bit_offset&0x07));bit++){
|
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-09-06 15:06:41 +00:00
|
|
|
}
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '.';
|
|
|
|
str_p++;
|
2009-09-06 15:06:41 +00:00
|
|
|
}
|
2009-05-15 18:38:13 +00:00
|
|
|
|
|
|
|
/* read the bits for the int */
|
2020-11-06 01:37:13 +00:00
|
|
|
for(i=0;i<max_bits;i++){
|
2009-05-15 18:38:13 +00:00
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
if(bit&&(!(bit%8))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
bit++;
|
2020-11-06 01:37:13 +00:00
|
|
|
if((value & mask) != 0){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '1';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
} else {
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '0';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
mask = mask>>1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;bit%8;bit++){
|
|
|
|
if(bit&&(!(bit%4))){
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = ' ';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
2012-12-09 23:12:59 +00:00
|
|
|
str[str_p] = '.';
|
|
|
|
str_p++;
|
2009-05-15 18:38:13 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2002-12-08 02:32:36 +00:00
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
gchar *
|
2015-01-08 00:24:52 +00:00
|
|
|
guid_to_str(wmem_allocator_t *scope, const e_guid_t *guid)
|
2014-09-04 01:39:04 +00:00
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
gchar *buf;
|
2005-07-04 13:04:53 +00:00
|
|
|
|
2015-01-08 00:24:52 +00:00
|
|
|
buf=(gchar *)wmem_alloc(scope, GUID_STR_LEN);
|
2013-12-19 15:27:44 +00:00
|
|
|
return guid_to_str_buf(guid, buf, GUID_STR_LEN);
|
2005-07-04 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
gchar *
|
|
|
|
guid_to_str_buf(const e_guid_t *guid, gchar *buf, int buf_len)
|
|
|
|
{
|
2013-12-19 15:27:44 +00:00
|
|
|
char *tempptr = buf;
|
|
|
|
|
|
|
|
if (buf_len < GUID_STR_LEN) {
|
2021-04-30 10:18:25 +00:00
|
|
|
(void) g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len);/* Let the unexpected value alert user */
|
2013-12-19 15:27:44 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 37 bytes */
|
|
|
|
tempptr = dword_to_hex(tempptr, guid->data1); /* 8 bytes */
|
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
2014-07-30 02:46:32 +00:00
|
|
|
tempptr = word_to_hex(tempptr, guid->data2); /* 4 bytes */
|
2013-12-19 15:27:44 +00:00
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
2014-07-30 02:46:32 +00:00
|
|
|
tempptr = word_to_hex(tempptr, guid->data3); /* 4 bytes */
|
2013-12-19 15:27:44 +00:00
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
|
|
|
tempptr = bytes_to_hexstr(tempptr, &guid->data4[0], 2); /* 4 bytes */
|
|
|
|
*tempptr++ = '-'; /* 1 byte */
|
|
|
|
tempptr = bytes_to_hexstr(tempptr, &guid->data4[2], 6); /* 12 bytes */
|
|
|
|
|
|
|
|
*tempptr = '\0';
|
|
|
|
return buf;
|
2005-07-04 13:04:53 +00:00
|
|
|
}
|
2013-06-17 21:54:21 +00:00
|
|
|
|
2014-09-04 01:39:04 +00:00
|
|
|
const gchar *
|
|
|
|
port_type_to_str (port_type type)
|
|
|
|
{
|
2013-06-17 21:54:21 +00:00
|
|
|
switch (type) {
|
|
|
|
case PT_NONE: return "NONE";
|
|
|
|
case PT_SCTP: return "SCTP";
|
|
|
|
case PT_TCP: return "TCP";
|
|
|
|
case PT_UDP: return "UDP";
|
|
|
|
case PT_DCCP: return "DCCP";
|
|
|
|
case PT_IPX: return "IPX";
|
|
|
|
case PT_DDP: return "DDP";
|
|
|
|
case PT_IDP: return "IDP";
|
|
|
|
case PT_USB: return "USB";
|
|
|
|
case PT_I2C: return "I2C";
|
|
|
|
case PT_IBQP: return "IBQP";
|
|
|
|
case PT_BLUETOOTH: return "BLUETOOTH";
|
2021-08-25 20:15:39 +00:00
|
|
|
case PT_IWARP_MPA: return "IWARP_MPA";
|
2014-07-30 02:46:32 +00:00
|
|
|
default: return "[Unknown]";
|
2013-06-17 21:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 19:34:48 +00:00
|
|
|
|
2013-12-19 15:27:44 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2013-12-19 15:27:44 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|