diff options
author | Guy Harris <guy@alum.mit.edu> | 2001-05-31 06:48:00 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2001-05-31 06:48:00 +0000 |
commit | 52025d69565657ebe1065c2ed74fe7811aee0dda (patch) | |
tree | b6f6edbff0b1611ecee3c4fcd13e84a303469075 | |
parent | 15336e821c21b86ffa8a31d4049e25a6b2472987 (diff) |
Display all the digits of the microsecond field of an absolute time
value.
Check that the microseconds field of an absolute time is valid, if it's
present.
Set "tm_isdst" in the "struct tm" handed to "mktime()" to -1, so that
"mktime()" will attempt to figure out whether the time is daylight
savings time or not.
Check that "mktime()" was able to convert the time.
svn path=/trunk/; revision=3487
-rw-r--r-- | epan/ftypes/ftype-time.c | 78 | ||||
-rw-r--r-- | epan/to_str.c | 8 |
2 files changed, 63 insertions, 23 deletions
diff --git a/epan/ftypes/ftype-time.c b/epan/ftypes/ftype-time.c index 7dcaf23b97..156b59d68a 100644 --- a/epan/ftypes/ftype-time.c +++ b/epan/ftypes/ftype-time.c @@ -1,8 +1,8 @@ /* - * $Id: ftype-time.c,v 1.5 2001/05/31 06:20:10 guy Exp $ + * $Id: ftype-time.c,v 1.6 2001/05/31 06:48:00 guy Exp $ * * Ethereal - Network traffic analyzer - * By Gerald Combs <gerald@zing.org> + * By Gerald Combs <gerald@ethereal.com> * Copyright 2001 Gerald Combs * * @@ -25,6 +25,8 @@ #include "config.h" #endif +#include <ctype.h> + #include <time.h> #include <ftypes-int.h> @@ -107,11 +109,8 @@ relative_val_from_string(fvalue_t *fv, char *s, LogFunc log) * Get the seconds value. */ fv->value.time.tv_sec = strtoul(curptr, &endptr, 10); - if (endptr == curptr || (*endptr != '\0' && *endptr != '.')) { - if (log != NULL) - log("\"%s\" is not a valid time.", s); - return FALSE; - } + if (endptr == curptr || (*endptr != '\0' && *endptr != '.')) + goto fail; curptr = endptr; if (*curptr == '.') curptr++; /* skip the decimal point */ @@ -132,11 +131,8 @@ relative_val_from_string(fvalue_t *fv, char *s, LogFunc log) * Get the microseconds value. */ fv->value.time.tv_usec = strtoul(curptr, &endptr, 10); - if (endptr == curptr || *endptr != '\0') { - if (log != NULL) - log("\"%s\" is not a valid time.", s); - return FALSE; - } + if (endptr == curptr || *endptr != '\0') + goto fail; } else { /* * No microseconds value - it's 0. @@ -148,6 +144,11 @@ relative_val_from_string(fvalue_t *fv, char *s, LogFunc log) * XXX - what about negative values? */ return TRUE; + +fail: + if (log != NULL) + log("\"%s\" is not a valid time.", s); + return FALSE; } @@ -155,15 +156,54 @@ static gboolean absolute_val_from_string(fvalue_t *fv, char *s, LogFunc log) { struct tm tm; - char *str; - str=strptime(s,"%b %d, %Y %H:%M:%S.",&tm); - if (!str) { - log("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"",s); - return FALSE; - } + char *curptr, *endptr; + + curptr = strptime(s,"%b %d, %Y %H:%M:%S", &tm); + if (curptr == NULL) + goto fail; + tm.tm_isdst = -1; /* let the computer figure out if it's DST */ fv->value.time.tv_sec = mktime(&tm); - sscanf(str,"%lu",&fv->value.time.tv_usec); + if (*curptr != '\0') { + /* + * Something came after the seconds field; it must be + * a microseconds field. + */ + if (*curptr != '.') + goto fail; /* it's not */ + curptr++; /* skip the "." */ + if (!isdigit((unsigned char)*curptr)) + goto fail; /* not a digit, so not valid */ + fv->value.time.tv_usec = strtoul(curptr, &endptr, 10); + if (endptr == curptr || *endptr != '\0') + goto fail; + } else { + /* + * No microseconds value - it's 0. + */ + fv->value.time.tv_usec = 0; + } + + if (fv->value.time.tv_sec == -1) { + /* + * XXX - should we supply an error message that mentions + * that the time specified might be syntactically valid + * but might not actually have occurred, e.g. a time in + * the non-existent time range after the clocks are + * set forward during daylight savings time (or possibly + * that it's in the time range after the clocks are set + * backward, so that there are two different times that + * it could be)? + */ + goto fail; + } + return TRUE; + +fail: + if (log != NULL) + log("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"", + s); + return FALSE; } static void diff --git a/epan/to_str.c b/epan/to_str.c index be282e1c1b..b40628bff8 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -1,7 +1,7 @@ /* to_str.h * Routines for utilities to convert various other types to strings. * - * $Id: to_str.c,v 1.7 2001/05/28 20:12:30 guy Exp $ + * $Id: to_str.c,v 1.8 2001/05/31 06:47:59 guy Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -340,7 +340,7 @@ abs_time_to_str(struct timeval *abs_time) { struct tm *tmp; static gchar *cur; - static char str[3][3+1+2+2+4+1+2+1+2+1+2+1+4+1 + 5 /* extra */]; + static char str[3][3+1+2+2+4+1+2+1+2+1+2+1+6+1 + 5 /* extra */]; if (cur == &str[0][0]) { cur = &str[1][0]; @@ -351,14 +351,14 @@ abs_time_to_str(struct timeval *abs_time) } tmp = localtime(&abs_time->tv_sec); - sprintf(cur, "%s %2d, %d %02d:%02d:%02d.%04ld", + sprintf(cur, "%s %2d, %d %02d:%02d:%02d.%06ld", mon_names[tmp->tm_mon], tmp->tm_mday, tmp->tm_year + 1900, tmp->tm_hour, tmp->tm_min, tmp->tm_sec, - (long)abs_time->tv_usec/100); + (long)abs_time->tv_usec); return cur; } |