4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 * Just make sure we include the prototype for strptime as well
32 * (needed for glibc 2.2) but make sure we do this only if not
41 #include <ftypes-int.h>
42 #include <epan/to_str.h>
44 #ifdef NEED_STRPTIME_H
45 #include "wsutil/strptime.h"
49 cmp_eq(const fvalue_t
*a
, const fvalue_t
*b
)
51 return ((a
->value
.time
.secs
) ==(b
->value
.time
.secs
))
52 &&((a
->value
.time
.nsecs
)==(b
->value
.time
.nsecs
));
55 cmp_ne(const fvalue_t
*a
, const fvalue_t
*b
)
57 return (a
->value
.time
.secs
!=b
->value
.time
.secs
)
58 ||(a
->value
.time
.nsecs
!=b
->value
.time
.nsecs
);
61 cmp_gt(const fvalue_t
*a
, const fvalue_t
*b
)
63 if (a
->value
.time
.secs
> b
->value
.time
.secs
) {
66 if (a
->value
.time
.secs
< b
->value
.time
.secs
) {
70 return a
->value
.time
.nsecs
> b
->value
.time
.nsecs
;
73 cmp_ge(const fvalue_t
*a
, const fvalue_t
*b
)
75 if (a
->value
.time
.secs
> b
->value
.time
.secs
) {
78 if (a
->value
.time
.secs
< b
->value
.time
.secs
) {
82 return a
->value
.time
.nsecs
>= b
->value
.time
.nsecs
;
85 cmp_lt(const fvalue_t
*a
, const fvalue_t
*b
)
87 if (a
->value
.time
.secs
< b
->value
.time
.secs
) {
90 if (a
->value
.time
.secs
> b
->value
.time
.secs
) {
94 return a
->value
.time
.nsecs
< b
->value
.time
.nsecs
;
97 cmp_le(const fvalue_t
*a
, const fvalue_t
*b
)
99 if (a
->value
.time
.secs
< b
->value
.time
.secs
) {
102 if (a
->value
.time
.secs
> b
->value
.time
.secs
) {
106 return a
->value
.time
.nsecs
<= b
->value
.time
.nsecs
;
111 * Get a nanoseconds value, starting at "p".
113 * Returns true on success, false on failure.
116 get_nsecs(char *startp
, int *nsecs
)
126 * How many characters are in the string?
128 ndigits
= (int)strlen(startp
);
131 * If there are N characters in the string, the last of the
132 * characters would be the digit corresponding to 10^(9-N)
138 * Start at the last character, and work backwards.
140 p
= startp
+ ndigits
;
142 while (p
!= startp
) {
145 if (!isdigit((unsigned char)*p
)) {
147 * Not a digit - error.
154 * Non-zero digit corresponding to that number
155 * of (10^scale) units.
157 * If scale is less than zero, this digit corresponds
158 * to a value less than a nanosecond, so this number
163 for (i
= 0; i
< scale
; i
++)
174 relative_val_from_unparsed(fvalue_t
*fv
, char *s
, gboolean allow_partial_value _U_
, LogFunc logfunc
)
176 char *curptr
, *endptr
;
177 gboolean negative
= FALSE
;
187 * If it doesn't begin with ".", it should contain a seconds
190 if (*curptr
!= '.') {
192 * Get the seconds value.
194 fv
->value
.time
.secs
= strtoul(curptr
, &endptr
, 10);
195 if (endptr
== curptr
|| (*endptr
!= '\0' && *endptr
!= '.'))
199 curptr
++; /* skip the decimal point */
202 * No seconds value - it's 0.
204 fv
->value
.time
.secs
= 0;
205 curptr
++; /* skip the decimal point */
209 * If there's more stuff left in the string, it should be the
212 if (*curptr
!= '\0') {
214 * Get the nanoseconds value.
216 if (!get_nsecs(curptr
, &fv
->value
.time
.nsecs
))
220 * No nanoseconds value - it's 0.
222 fv
->value
.time
.nsecs
= 0;
226 fv
->value
.time
.secs
= -fv
->value
.time
.secs
;
227 fv
->value
.time
.nsecs
= -fv
->value
.time
.nsecs
;
233 logfunc("\"%s\" is not a valid time.", s
);
239 absolute_val_from_string(fvalue_t
*fv
, char *s
, LogFunc logfunc
)
244 memset(&tm
, 0, sizeof(tm
));
245 curptr
= strptime(s
,"%b %d, %Y %H:%M:%S", &tm
);
247 curptr
= strptime(s
,"%Y-%m-%dT%H:%M:%S", &tm
);
249 curptr
= strptime(s
,"%Y-%m-%d %H:%M:%S", &tm
);
251 curptr
= strptime(s
,"%Y-%m-%d %H:%M", &tm
);
253 curptr
= strptime(s
,"%Y-%m-%d %H", &tm
);
255 curptr
= strptime(s
,"%Y-%m-%d", &tm
);
258 tm
.tm_isdst
= -1; /* let the computer figure out if it's DST */
259 fv
->value
.time
.secs
= mktime(&tm
);
260 if (*curptr
!= '\0') {
262 * Something came after the seconds field; it must be
263 * a nanoseconds field.
266 goto fail
; /* it's not */
267 curptr
++; /* skip the "." */
268 if (!isdigit((unsigned char)*curptr
))
269 goto fail
; /* not a digit, so not valid */
270 if (!get_nsecs(curptr
, &fv
->value
.time
.nsecs
))
274 * No nanoseconds value - it's 0.
276 fv
->value
.time
.nsecs
= 0;
279 if (fv
->value
.time
.secs
== -1) {
281 * XXX - should we supply an error message that mentions
282 * that the time specified might be syntactically valid
283 * but might not actually have occurred, e.g. a time in
284 * the non-existent time range after the clocks are
285 * set forward during daylight savings time (or possibly
286 * that it's in the time range after the clocks are set
287 * backward, so that there are two different times that
297 logfunc("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\" or \"2011-07-04 12:34:56\"",
303 absolute_val_from_unparsed(fvalue_t
*fv
, char *s
, gboolean allow_partial_value _U_
, LogFunc logfunc
)
305 return absolute_val_from_string(fv
, s
, logfunc
);
309 time_fvalue_new(fvalue_t
*fv
)
311 fv
->value
.time
.secs
= 0;
312 fv
->value
.time
.nsecs
= 0;
316 time_fvalue_set(fvalue_t
*fv
, gpointer value
, gboolean already_copied
)
318 g_assert(!already_copied
);
319 memcpy(&(fv
->value
.time
), value
, sizeof(nstime_t
));
323 value_get(fvalue_t
*fv
)
325 return &(fv
->value
.time
);
329 absolute_val_repr_len(fvalue_t
*fv
, ftrepr_t rtype _U_
)
333 rep
= abs_time_to_str(&fv
->value
.time
, ABSOLUTE_TIME_LOCAL
,
334 rtype
== FTREPR_DISPLAY
);
335 return (int)strlen(rep
) + 2; /* 2 for opening and closing quotes */
339 absolute_val_to_repr(fvalue_t
*fv
, ftrepr_t rtype _U_
, char *buf
)
341 sprintf(buf
, "\"%s\"",
342 abs_time_to_str(&fv
->value
.time
, ABSOLUTE_TIME_LOCAL
,
343 rtype
== FTREPR_DISPLAY
));
347 relative_val_repr_len(fvalue_t
*fv
, ftrepr_t rtype _U_
)
351 rep
= rel_time_to_secs_str(&fv
->value
.time
);
352 return (int)strlen(rep
);
356 relative_val_to_repr(fvalue_t
*fv
, ftrepr_t rtype _U_
, char *buf
)
358 strcpy(buf
, rel_time_to_secs_str(&fv
->value
.time
));
362 ftype_register_time(void)
365 static ftype_t abstime_type
= {
366 FT_ABSOLUTE_TIME
, /* ftype */
367 "FT_ABSOLUTE_TIME", /* name */
368 "Date and time", /* pretty_name */
370 time_fvalue_new
, /* new_value */
371 NULL
, /* free_value */
372 absolute_val_from_unparsed
, /* val_from_unparsed */
373 absolute_val_from_string
, /* val_from_string */
374 absolute_val_to_repr
, /* val_to_string_repr */
375 absolute_val_repr_len
, /* len_string_repr */
377 time_fvalue_set
, /* set_value */
378 NULL
, /* set_value_uinteger */
379 NULL
, /* set_value_sinteger */
380 NULL
, /* set_value_integer64 */
381 NULL
, /* set_value_floating */
383 value_get
, /* get_value */
384 NULL
, /* get_value_uinteger */
385 NULL
, /* get_value_sinteger */
386 NULL
, /* get_value_integer64 */
387 NULL
, /* get_value_floating */
395 NULL
, /* cmp_bitwise_and */
396 NULL
, /* cmp_contains */
397 NULL
, /* cmp_matches */
402 static ftype_t reltime_type
= {
403 FT_RELATIVE_TIME
, /* ftype */
404 "FT_RELATIVE_TIME", /* name */
405 "Time offset", /* pretty_name */
407 time_fvalue_new
, /* new_value */
408 NULL
, /* free_value */
409 relative_val_from_unparsed
, /* val_from_unparsed */
410 NULL
, /* val_from_string */
411 relative_val_to_repr
, /* val_to_string_repr */
412 relative_val_repr_len
, /* len_string_repr */
414 time_fvalue_set
, /* set_value */
415 NULL
, /* set_value_uinteger */
416 NULL
, /* set_value_sinteger */
417 NULL
, /* set_value_integer64 */
418 NULL
, /* set_value_floating */
420 value_get
, /* get_value */
421 NULL
, /* get_value_uinteger */
422 NULL
, /* get_value_sinteger */
423 NULL
, /* get_value_integer64 */
424 NULL
, /* get_value_floating */
432 NULL
, /* cmp_bitwise_and */
433 NULL
, /* cmp_contains */
434 NULL
, /* cmp_matches */
440 ftype_register(FT_ABSOLUTE_TIME
, &abstime_type
);
441 ftype_register(FT_RELATIVE_TIME
, &reltime_type
);