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.
31 * Just make sure we include the prototype for strptime as well
32 * (needed for glibc 2.2)
38 #include <ftypes-int.h>
40 #ifdef NEED_STRPTIME_H
45 cmp_eq(fvalue_t *a, fvalue_t *b)
47 return ((a->value.time.secs) ==(b->value.time.secs))
48 &&((a->value.time.nsecs)==(b->value.time.nsecs));
51 cmp_ne(fvalue_t *a, fvalue_t *b)
53 return (a->value.time.secs !=b->value.time.secs)
54 ||(a->value.time.nsecs!=b->value.time.nsecs);
57 cmp_gt(fvalue_t *a, fvalue_t *b)
59 if (a->value.time.secs > b->value.time.secs) {
62 if (a->value.time.secs < b->value.time.secs) {
66 return a->value.time.nsecs > b->value.time.nsecs;
69 cmp_ge(fvalue_t *a, fvalue_t *b)
71 if (a->value.time.secs > b->value.time.secs) {
74 if (a->value.time.secs < b->value.time.secs) {
78 return a->value.time.nsecs >= b->value.time.nsecs;
81 cmp_lt(fvalue_t *a, fvalue_t *b)
83 if (a->value.time.secs < b->value.time.secs) {
86 if (a->value.time.secs > b->value.time.secs) {
90 return a->value.time.nsecs < b->value.time.nsecs;
93 cmp_le(fvalue_t *a, fvalue_t *b)
95 if (a->value.time.secs < b->value.time.secs) {
98 if (a->value.time.secs > b->value.time.secs) {
102 return a->value.time.nsecs <= b->value.time.nsecs;
107 * Get a nanoseconds value, starting at "p".
109 * Returns true on success, false on failure.
112 get_nsecs(char *startp, int *nsecs)
122 * How many characters are in the string?
124 ndigits = strlen(startp);
127 * If there are N characters in the string, the last of the
128 * characters would be the digit corresponding to 10^(9-N)
134 * Start at the last character, and work backwards.
136 p = startp + ndigits;
138 while (p != startp) {
141 if (!isdigit((unsigned char)*p)) {
143 * Not a digit - error.
150 * Non-zero digit corresponding to that number
151 * of (10^scale) units.
153 * If scale is less than zero, this digit corresponds
154 * to a value less than a nanosecond, so this number
159 for (i = 0; i < scale; i++)
170 relative_val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
172 char *curptr, *endptr;
173 gboolean negative = FALSE;
183 * If it doesn't begin with ".", it should contain a seconds
186 if (*curptr != '.') {
188 * Get the seconds value.
190 fv->value.time.secs = strtoul(curptr, &endptr, 10);
191 if (endptr == curptr || (*endptr != '\0' && *endptr != '.'))
195 curptr++; /* skip the decimal point */
198 * No seconds value - it's 0.
200 fv->value.time.secs = 0;
201 curptr++; /* skip the decimal point */
205 * If there's more stuff left in the string, it should be the
208 if (*curptr != '\0') {
210 * Get the nanoseconds value.
212 if (!get_nsecs(curptr, &fv->value.time.nsecs))
216 * No nanoseconds value - it's 0.
218 fv->value.time.nsecs = 0;
222 fv->value.time.secs = -fv->value.time.secs;
223 fv->value.time.nsecs = -fv->value.time.nsecs;
229 logfunc("\"%s\" is not a valid time.", s);
235 absolute_val_from_string(fvalue_t *fv, char *s, LogFunc logfunc)
240 curptr = strptime(s,"%b %d, %Y %H:%M:%S", &tm);
243 tm.tm_isdst = -1; /* let the computer figure out if it's DST */
244 fv->value.time.secs = mktime(&tm);
245 if (*curptr != '\0') {
247 * Something came after the seconds field; it must be
248 * a nanoseconds field.
251 goto fail; /* it's not */
252 curptr++; /* skip the "." */
253 if (!isdigit((unsigned char)*curptr))
254 goto fail; /* not a digit, so not valid */
255 if (!get_nsecs(curptr, &fv->value.time.nsecs))
259 * No nanoseconds value - it's 0.
261 fv->value.time.nsecs = 0;
264 if (fv->value.time.secs == -1) {
266 * XXX - should we supply an error message that mentions
267 * that the time specified might be syntactically valid
268 * but might not actually have occurred, e.g. a time in
269 * the non-existent time range after the clocks are
270 * set forward during daylight savings time (or possibly
271 * that it's in the time range after the clocks are set
272 * backward, so that there are two different times that
282 logfunc("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"",
288 absolute_val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
290 return absolute_val_from_string(fv, s, logfunc);
294 time_fvalue_new(fvalue_t *fv)
296 fv->value.time.secs = 0;
297 fv->value.time.nsecs = 0;
301 time_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
303 g_assert(!already_copied);
304 memcpy(&(fv->value.time), value, sizeof(nstime_t));
308 value_get(fvalue_t *fv)
310 return &(fv->value.time);
314 absolute_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
318 rep = abs_time_to_str(&fv->value.time);
319 return strlen(rep) + 2; /* 2 for opening and closing quotes */
323 absolute_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
325 sprintf(buf, "\"%s\"", abs_time_to_str(&fv->value.time));
329 relative_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
333 rep = rel_time_to_secs_str(&fv->value.time);
338 relative_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
340 strcpy(buf, rel_time_to_secs_str(&fv->value.time));
344 ftype_register_time(void)
347 static ftype_t abstime_type = {
348 FT_ABSOLUTE_TIME, /* ftype */
349 "FT_ABSOLUTE_TIME", /* name */
350 "date/time", /* pretty_name */
352 time_fvalue_new, /* new_value */
353 NULL, /* free_value */
354 absolute_val_from_unparsed, /* val_from_unparsed */
355 absolute_val_from_string, /* val_from_string */
356 absolute_val_to_repr, /* val_to_string_repr */
357 absolute_val_repr_len, /* len_string_repr */
359 time_fvalue_set, /* set_value */
360 NULL, /* set_value_integer */
361 NULL, /* set_value_integer64 */
362 NULL, /* set_value_floating */
364 value_get, /* get_value */
365 NULL, /* get_value_integer */
366 NULL, /* get_value_integer64 */
367 NULL, /* get_value_floating */
375 NULL, /* cmp_bitwise_and */
376 NULL, /* cmp_contains */
377 NULL, /* cmp_matches */
382 static ftype_t reltime_type = {
383 FT_RELATIVE_TIME, /* ftype */
384 "FT_RELATIVE_TIME", /* name */
385 "time offset", /* pretty_name */
387 time_fvalue_new, /* new_value */
388 NULL, /* free_value */
389 relative_val_from_unparsed, /* val_from_unparsed */
390 NULL, /* val_from_string */
391 relative_val_to_repr, /* val_to_string_repr */
392 relative_val_repr_len, /* len_string_repr */
394 time_fvalue_set, /* set_value */
395 NULL, /* set_value_integer */
396 NULL, /* set_value_integer64 */
397 NULL, /* set_value_floating */
399 value_get, /* get_value */
400 NULL, /* get_value_integer */
401 NULL, /* get_value_integer64 */
402 NULL, /* get_value_floating */
410 NULL, /* cmp_bitwise_and */
411 NULL, /* cmp_contains */
412 NULL, /* cmp_matches */
418 ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
419 ftype_register(FT_RELATIVE_TIME, &reltime_type);