4 * Ethereal - Network traffic analyzer
5 * By Gerald Combs <gerald@ethereal.com>
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;
177 * If it doesn't begin with ".", it should contain a seconds
180 if (*curptr != '.') {
182 * Get the seconds value.
184 fv->value.time.secs = strtoul(curptr, &endptr, 10);
185 if (endptr == curptr || (*endptr != '\0' && *endptr != '.'))
189 curptr++; /* skip the decimal point */
192 * No seconds value - it's 0.
194 fv->value.time.secs = 0;
195 curptr++; /* skip the decimal point */
199 * If there's more stuff left in the string, it should be the
202 if (*curptr != '\0') {
204 * Get the nanoseconds value.
206 if (!get_nsecs(curptr, &fv->value.time.nsecs))
210 * No nanoseconds value - it's 0.
212 fv->value.time.nsecs = 0;
216 * XXX - what about negative values?
222 logfunc("\"%s\" is not a valid time.", s);
228 absolute_val_from_string(fvalue_t *fv, char *s, LogFunc logfunc)
233 curptr = strptime(s,"%b %d, %Y %H:%M:%S", &tm);
236 tm.tm_isdst = -1; /* let the computer figure out if it's DST */
237 fv->value.time.secs = mktime(&tm);
238 if (*curptr != '\0') {
240 * Something came after the seconds field; it must be
241 * a nanoseconds field.
244 goto fail; /* it's not */
245 curptr++; /* skip the "." */
246 if (!isdigit((unsigned char)*curptr))
247 goto fail; /* not a digit, so not valid */
248 if (!get_nsecs(curptr, &fv->value.time.nsecs))
252 * No nanoseconds value - it's 0.
254 fv->value.time.nsecs = 0;
257 if (fv->value.time.secs == -1) {
259 * XXX - should we supply an error message that mentions
260 * that the time specified might be syntactically valid
261 * but might not actually have occurred, e.g. a time in
262 * the non-existent time range after the clocks are
263 * set forward during daylight savings time (or possibly
264 * that it's in the time range after the clocks are set
265 * backward, so that there are two different times that
275 logfunc("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"",
281 absolute_val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
283 return absolute_val_from_string(fv, s, logfunc);
287 time_fvalue_new(fvalue_t *fv)
289 fv->value.time.secs = 0;
290 fv->value.time.nsecs = 0;
294 time_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
296 g_assert(!already_copied);
297 memcpy(&(fv->value.time), value, sizeof(nstime_t));
301 value_get(fvalue_t *fv)
303 return &(fv->value.time);
307 absolute_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
311 rep = abs_time_to_str(&fv->value.time);
312 return strlen(rep) + 2; /* 2 for opening and closing quotes */
316 absolute_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
318 sprintf(buf, "\"%s\"", abs_time_to_str(&fv->value.time));
322 relative_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
326 rep = rel_time_to_secs_str(&fv->value.time);
331 relative_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
333 strcpy(buf, rel_time_to_secs_str(&fv->value.time));
337 ftype_register_time(void)
340 static ftype_t abstime_type = {
341 "FT_ABSOLUTE_TIME", /* name */
342 "date/time", /* pretty_name */
344 time_fvalue_new, /* new_value */
345 NULL, /* free_value */
346 absolute_val_from_unparsed, /* val_from_unparsed */
347 absolute_val_from_string, /* val_from_string */
348 absolute_val_to_repr, /* val_to_string_repr */
349 absolute_val_repr_len, /* len_string_repr */
351 time_fvalue_set, /* set_value */
352 NULL, /* set_value_integer */
353 NULL, /* set_value_integer64 */
354 NULL, /* set_value_floating */
356 value_get, /* get_value */
357 NULL, /* get_value_integer */
358 NULL, /* get_value_integer64 */
359 NULL, /* get_value_floating */
367 NULL, /* cmp_bitwise_and */
368 NULL, /* cmp_contains */
369 NULL, /* cmp_matches */
374 static ftype_t reltime_type = {
375 "FT_RELATIVE_TIME", /* name */
376 "time offset", /* pretty_name */
378 time_fvalue_new, /* new_value */
379 NULL, /* free_value */
380 relative_val_from_unparsed, /* val_from_unparsed */
381 NULL, /* val_from_string */
382 relative_val_to_repr, /* val_to_string_repr */
383 relative_val_repr_len, /* len_string_repr */
385 time_fvalue_set, /* set_value */
386 NULL, /* set_value_integer */
387 NULL, /* set_value_integer64 */
388 NULL, /* set_value_floating */
390 value_get, /* get_value */
391 NULL, /* get_value_integer */
392 NULL, /* get_value_integer64 */
393 NULL, /* get_value_floating */
401 NULL, /* cmp_bitwise_and */
402 NULL, /* cmp_contains */
403 NULL, /* cmp_matches */
409 ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
410 ftype_register(FT_RELATIVE_TIME, &reltime_type);