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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 * Just make sure we include the prototype for strptime as well
33 * (needed for glibc 2.2) but make sure we do this only if not
42 #include <ftypes-int.h>
44 #ifdef NEED_STRPTIME_H
45 #include "wsutil/strptime.h"
49 cmp_eq(fvalue_t *a, 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(fvalue_t *a, 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(fvalue_t *a, 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(fvalue_t *a, 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(fvalue_t *a, 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(fvalue_t *a, 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 curptr = strptime(s,"%b %d, %Y %H:%M:%S", &tm);
247 tm.tm_isdst = -1; /* let the computer figure out if it's DST */
248 fv->value.time.secs = mktime(&tm);
249 if (*curptr != '\0') {
251 * Something came after the seconds field; it must be
252 * a nanoseconds field.
255 goto fail; /* it's not */
256 curptr++; /* skip the "." */
257 if (!isdigit((unsigned char)*curptr))
258 goto fail; /* not a digit, so not valid */
259 if (!get_nsecs(curptr, &fv->value.time.nsecs))
263 * No nanoseconds value - it's 0.
265 fv->value.time.nsecs = 0;
268 if (fv->value.time.secs == -1) {
270 * XXX - should we supply an error message that mentions
271 * that the time specified might be syntactically valid
272 * but might not actually have occurred, e.g. a time in
273 * the non-existent time range after the clocks are
274 * set forward during daylight savings time (or possibly
275 * that it's in the time range after the clocks are set
276 * backward, so that there are two different times that
286 logfunc("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"",
292 absolute_val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
294 return absolute_val_from_string(fv, s, logfunc);
298 time_fvalue_new(fvalue_t *fv)
300 fv->value.time.secs = 0;
301 fv->value.time.nsecs = 0;
305 time_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
307 g_assert(!already_copied);
308 memcpy(&(fv->value.time), value, sizeof(nstime_t));
312 value_get(fvalue_t *fv)
314 return &(fv->value.time);
318 absolute_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
322 rep = abs_time_to_str(&fv->value.time, ABSOLUTE_TIME_LOCAL,
323 rtype == FTREPR_DISPLAY);
324 return (int)strlen(rep) + 2; /* 2 for opening and closing quotes */
328 absolute_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
330 sprintf(buf, "\"%s\"",
331 abs_time_to_str(&fv->value.time, ABSOLUTE_TIME_LOCAL,
332 rtype == FTREPR_DISPLAY));
336 relative_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
340 rep = rel_time_to_secs_str(&fv->value.time);
341 return (int)strlen(rep);
345 relative_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
347 strcpy(buf, rel_time_to_secs_str(&fv->value.time));
351 ftype_register_time(void)
354 static ftype_t abstime_type = {
355 FT_ABSOLUTE_TIME, /* ftype */
356 "FT_ABSOLUTE_TIME", /* name */
357 "date/time", /* pretty_name */
359 time_fvalue_new, /* new_value */
360 NULL, /* free_value */
361 absolute_val_from_unparsed, /* val_from_unparsed */
362 absolute_val_from_string, /* val_from_string */
363 absolute_val_to_repr, /* val_to_string_repr */
364 absolute_val_repr_len, /* len_string_repr */
366 time_fvalue_set, /* set_value */
367 NULL, /* set_value_uinteger */
368 NULL, /* set_value_sinteger */
369 NULL, /* set_value_integer64 */
370 NULL, /* set_value_floating */
372 value_get, /* get_value */
373 NULL, /* get_value_uinteger */
374 NULL, /* get_value_sinteger */
375 NULL, /* get_value_integer64 */
376 NULL, /* get_value_floating */
384 NULL, /* cmp_bitwise_and */
385 NULL, /* cmp_contains */
386 NULL, /* cmp_matches */
391 static ftype_t reltime_type = {
392 FT_RELATIVE_TIME, /* ftype */
393 "FT_RELATIVE_TIME", /* name */
394 "time offset", /* pretty_name */
396 time_fvalue_new, /* new_value */
397 NULL, /* free_value */
398 relative_val_from_unparsed, /* val_from_unparsed */
399 NULL, /* val_from_string */
400 relative_val_to_repr, /* val_to_string_repr */
401 relative_val_repr_len, /* len_string_repr */
403 time_fvalue_set, /* set_value */
404 NULL, /* set_value_uinteger */
405 NULL, /* set_value_sinteger */
406 NULL, /* set_value_integer64 */
407 NULL, /* set_value_floating */
409 value_get, /* get_value */
410 NULL, /* get_value_uinteger */
411 NULL, /* get_value_sinteger */
412 NULL, /* get_value_integer64 */
413 NULL, /* get_value_floating */
421 NULL, /* cmp_bitwise_and */
422 NULL, /* cmp_contains */
423 NULL, /* cmp_matches */
429 ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
430 ftype_register(FT_RELATIVE_TIME, &reltime_type);