MSWSP: parse_CColumnGroupArray() etc.
[wireshark-wip.git] / epan / ftypes / ftype-time.c
blobe32a482173f5850aa0ad1a075466fafdcdbdbc71
1 /*
2 * $Id$
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.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
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
33 * yet defined.
35 #ifndef __USE_XOPEN
36 # define __USE_XOPEN
37 #endif
39 #include <time.h>
41 #include <ftypes-int.h>
42 #include <epan/to_str.h>
44 #ifdef NEED_STRPTIME_H
45 #include "wsutil/strptime.h"
46 #endif
48 static gboolean
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));
54 static gboolean
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);
60 static gboolean
61 cmp_gt(const fvalue_t *a, const fvalue_t *b)
63 if (a->value.time.secs > b->value.time.secs) {
64 return TRUE;
66 if (a->value.time.secs < b->value.time.secs) {
67 return FALSE;
70 return a->value.time.nsecs > b->value.time.nsecs;
72 static gboolean
73 cmp_ge(const fvalue_t *a, const fvalue_t *b)
75 if (a->value.time.secs > b->value.time.secs) {
76 return TRUE;
78 if (a->value.time.secs < b->value.time.secs) {
79 return FALSE;
82 return a->value.time.nsecs >= b->value.time.nsecs;
84 static gboolean
85 cmp_lt(const fvalue_t *a, const fvalue_t *b)
87 if (a->value.time.secs < b->value.time.secs) {
88 return TRUE;
90 if (a->value.time.secs > b->value.time.secs) {
91 return FALSE;
94 return a->value.time.nsecs < b->value.time.nsecs;
96 static gboolean
97 cmp_le(const fvalue_t *a, const fvalue_t *b)
99 if (a->value.time.secs < b->value.time.secs) {
100 return TRUE;
102 if (a->value.time.secs > b->value.time.secs) {
103 return FALSE;
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.
115 static gboolean
116 get_nsecs(char *startp, int *nsecs)
118 int ndigits;
119 int scale;
120 char *p;
121 int val;
122 int digit;
123 int i;
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)
133 * nanoseconds.
135 scale = 9 - ndigits;
138 * Start at the last character, and work backwards.
140 p = startp + ndigits;
141 val = 0;
142 while (p != startp) {
143 p--;
145 if (!isdigit((unsigned char)*p)) {
147 * Not a digit - error.
149 return FALSE;
151 digit = *p - '0';
152 if (digit != 0) {
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
159 * isn't valid.
161 if (scale < 0)
162 return FALSE;
163 for (i = 0; i < scale; i++)
164 digit *= 10;
165 val += digit;
167 scale++;
169 *nsecs = val;
170 return TRUE;
173 static gboolean
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;
179 curptr = s;
181 if(*curptr == '-') {
182 negative = TRUE;
183 curptr++;
187 * If it doesn't begin with ".", it should contain a seconds
188 * value.
190 if (*curptr != '.') {
192 * Get the seconds value.
194 fv->value.time.secs = strtoul(curptr, &endptr, 10);
195 if (endptr == curptr || (*endptr != '\0' && *endptr != '.'))
196 goto fail;
197 curptr = endptr;
198 if (*curptr == '.')
199 curptr++; /* skip the decimal point */
200 } else {
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
210 * nanoseconds value.
212 if (*curptr != '\0') {
214 * Get the nanoseconds value.
216 if (!get_nsecs(curptr, &fv->value.time.nsecs))
217 goto fail;
218 } else {
220 * No nanoseconds value - it's 0.
222 fv->value.time.nsecs = 0;
225 if(negative) {
226 fv->value.time.secs = -fv->value.time.secs;
227 fv->value.time.nsecs = -fv->value.time.nsecs;
229 return TRUE;
231 fail:
232 if (logfunc != NULL)
233 logfunc("\"%s\" is not a valid time.", s);
234 return FALSE;
238 static gboolean
239 absolute_val_from_string(fvalue_t *fv, char *s, LogFunc logfunc)
241 struct tm tm;
242 char *curptr;
244 memset(&tm, 0, sizeof(tm));
245 curptr = strptime(s,"%b %d, %Y %H:%M:%S", &tm);
246 if (curptr == NULL)
247 curptr = strptime(s,"%Y-%m-%dT%H:%M:%S", &tm);
248 if (curptr == NULL)
249 curptr = strptime(s,"%Y-%m-%d %H:%M:%S", &tm);
250 if (curptr == NULL)
251 curptr = strptime(s,"%Y-%m-%d %H:%M", &tm);
252 if (curptr == NULL)
253 curptr = strptime(s,"%Y-%m-%d %H", &tm);
254 if (curptr == NULL)
255 curptr = strptime(s,"%Y-%m-%d", &tm);
256 if (curptr == NULL)
257 goto fail;
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.
265 if (*curptr != '.')
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))
271 goto fail;
272 } else {
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
288 * it could be)?
290 goto fail;
293 return TRUE;
295 fail:
296 if (logfunc != NULL)
297 logfunc("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\" or \"2011-07-04 12:34:56\"",
299 return FALSE;
302 static gboolean
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);
308 static void
309 time_fvalue_new(fvalue_t *fv)
311 fv->value.time.secs = 0;
312 fv->value.time.nsecs = 0;
315 static void
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));
322 static gpointer
323 value_get(fvalue_t *fv)
325 return &(fv->value.time);
328 static int
329 absolute_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
331 gchar *rep;
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 */
338 static void
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));
346 static int
347 relative_val_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
349 gchar *rep;
351 rep = rel_time_to_secs_str(&fv->value.time);
352 return (int)strlen(rep);
355 static void
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));
361 void
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 */
369 0, /* wire_size */
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 */
389 cmp_eq,
390 cmp_ne,
391 cmp_gt,
392 cmp_ge,
393 cmp_lt,
394 cmp_le,
395 NULL, /* cmp_bitwise_and */
396 NULL, /* cmp_contains */
397 NULL, /* cmp_matches */
399 NULL,
400 NULL
402 static ftype_t reltime_type = {
403 FT_RELATIVE_TIME, /* ftype */
404 "FT_RELATIVE_TIME", /* name */
405 "Time offset", /* pretty_name */
406 0, /* wire_size */
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 */
426 cmp_eq,
427 cmp_ne,
428 cmp_gt,
429 cmp_ge,
430 cmp_lt,
431 cmp_le,
432 NULL, /* cmp_bitwise_and */
433 NULL, /* cmp_contains */
434 NULL, /* cmp_matches */
436 NULL,
437 NULL
440 ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
441 ftype_register(FT_RELATIVE_TIME, &reltime_type);