aboutsummaryrefslogtreecommitdiffstats
path: root/epan/to_str.c
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2021-12-29 00:45:25 +0000
committerWireshark GitLab Utility <gerald+gitlab-utility@wireshark.org>2021-12-29 13:50:21 +0000
commit62c9f333f7835c6be51d5aa7a6a5a589a295058d (patch)
tree8c0842a063731496ff48f3d7564a3c9899166fb6 /epan/to_str.c
parent64572a11f9849d072de15348338d749e0f885cd1 (diff)
epan: More abs_time_to_str() cleanups
Use abs_time_to_str() to implement abs_time_secs_to_str(). Misc cleanups.
Diffstat (limited to 'epan/to_str.c')
-rw-r--r--epan/to_str.c59
1 files changed, 27 insertions, 32 deletions
diff --git a/epan/to_str.c b/epan/to_str.c
index 2649255d94..d43e2b7ffa 100644
--- a/epan/to_str.c
+++ b/epan/to_str.c
@@ -126,8 +126,9 @@ get_fmt_zonename(field_display_e fmt, struct tm *tmp)
}
static char *
-snprint_abs_time_secs(wmem_allocator_t *scope, field_display_e fmt,
- struct tm *tmp, const char *trailer,
+snprint_abs_time_secs(wmem_allocator_t *scope,
+ field_display_e fmt, struct tm *tmp,
+ const char *nsecs_str, const char *tzone_str,
gboolean add_quotes)
{
char *buf;
@@ -135,21 +136,22 @@ snprint_abs_time_secs(wmem_allocator_t *scope, field_display_e fmt,
switch (fmt) {
case ABSOLUTE_TIME_DOY_UTC:
buf = wmem_strdup_printf(scope,
- "%s%04d/%03d:%02d:%02d:%02d%s%s",
+ "%s%04d/%03d:%02d:%02d:%02d%s%s%s",
add_quotes ? "\"" : "",
tmp->tm_year + 1900,
tmp->tm_yday + 1,
tmp->tm_hour,
tmp->tm_min,
tmp->tm_sec,
- trailer,
+ nsecs_str,
+ tzone_str,
add_quotes ? "\"" : "");
break;
case ABSOLUTE_TIME_NTP_UTC: /* FALLTHROUGH */
case ABSOLUTE_TIME_UTC: /* FALLTHROUGH */
case ABSOLUTE_TIME_LOCAL:
buf = wmem_strdup_printf(scope,
- "%s%s %2d, %d %02d:%02d:%02d%s%s",
+ "%s%s %2d, %d %02d:%02d:%02d%s%s%s",
add_quotes ? "\"" : "",
mon_names[tmp->tm_mon],
tmp->tm_mday,
@@ -157,7 +159,8 @@ snprint_abs_time_secs(wmem_allocator_t *scope, field_display_e fmt,
tmp->tm_hour,
tmp->tm_min,
tmp->tm_sec,
- trailer,
+ nsecs_str,
+ tzone_str,
add_quotes ? "\"" : "");
break;
default:
@@ -171,11 +174,13 @@ abs_time_to_str_ex(wmem_allocator_t *scope, const nstime_t *abs_time, field_disp
int flags)
{
struct tm *tmp;
- char buf_trailer[64];
+ char buf_nsecs[32];
+ char buf_tzone[32];
ws_assert(FIELD_DISPLAY_IS_ABSOLUTE_TIME(fmt));
- if (fmt == ABSOLUTE_TIME_NTP_UTC && nstime_is_zero(abs_time)) {
+ if (fmt == ABSOLUTE_TIME_NTP_UTC && abs_time->secs == 0 &&
+ (abs_time->nsecs == 0 || abs_time->nsecs == G_MAXINT)) {
return wmem_strdup(scope, "NULL");
}
@@ -184,38 +189,28 @@ abs_time_to_str_ex(wmem_allocator_t *scope, const nstime_t *abs_time, field_disp
return wmem_strdup(scope, "Not representable");
}
- if (flags & ABS_TIME_TO_STR_SHOW_ZONE)
- snprintf(buf_trailer, sizeof(buf_trailer), ".%09d %s", abs_time->nsecs, get_fmt_zonename(fmt, tmp));
- else
- snprintf(buf_trailer, sizeof(buf_trailer), ".%09d", abs_time->nsecs);
+ *buf_nsecs = '\0';
+ if (abs_time->nsecs != G_MAXINT) {
+ snprintf(buf_nsecs, sizeof(buf_nsecs), ".%09d", abs_time->nsecs);
+ }
+
+ *buf_tzone = '\0';
+ if (flags & ABS_TIME_TO_STR_SHOW_ZONE) {
+ snprintf(buf_tzone, sizeof(buf_tzone), " %s", get_fmt_zonename(fmt, tmp));
+ }
- return snprint_abs_time_secs(scope, fmt, tmp, buf_trailer, flags & ABS_TIME_TO_STR_ADD_DQUOTES);
+ return snprint_abs_time_secs(scope, fmt, tmp, buf_nsecs, buf_tzone, flags & ABS_TIME_TO_STR_ADD_DQUOTES);
}
char *
abs_time_secs_to_str_ex(wmem_allocator_t *scope, const time_t abs_time_secs, field_display_e fmt,
int flags)
{
- struct tm *tmp;
- char buf_trailer[64];
-
- ws_assert(FIELD_DISPLAY_IS_ABSOLUTE_TIME(fmt));
-
- if (fmt == ABSOLUTE_TIME_NTP_UTC && abs_time_secs == 0) {
- return wmem_strdup(scope, "NULL");
- }
-
- tmp = get_fmt_broken_down_time(fmt, &abs_time_secs);
- if (tmp == NULL) {
- return wmem_strdup(scope, "Not representable");
- }
-
- if (flags & ABS_TIME_TO_STR_SHOW_ZONE)
- snprintf(buf_trailer, sizeof(buf_trailer), " %s", get_fmt_zonename(fmt, tmp));
- else
- *buf_trailer = '\0';
+ nstime_t abs_time;
- return snprint_abs_time_secs(scope, fmt, tmp, buf_trailer, flags & ABS_TIME_TO_STR_ADD_DQUOTES);
+ nstime_set_unset(&abs_time);
+ abs_time.secs = abs_time_secs;
+ return abs_time_to_str_ex(scope, &abs_time, fmt, flags);
}
void