diff --git a/CHANGES b/CHANGES index 0b84f59178..aec6862400 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,9 @@ +2.1-84 | 2012-10-19 15:12:56 -0700 + + * Added a BiF strptime() to wrap the corresponding C function. (Seth + Hall) + 2.1-82 | 2012-10-19 15:05:40 -0700 * Add IPv6 support to signature header conditions. (Jon Siwek) diff --git a/VERSION b/VERSION index a6d8fd0427..c69ab9646c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1-82 +2.1-84 diff --git a/aux/broctl b/aux/broctl index 3d7c2d61e6..fd0e7e0b0c 160000 --- a/aux/broctl +++ b/aux/broctl @@ -1 +1 @@ -Subproject commit 3d7c2d61e63d7d51c455363cdfe9373e4e680b89 +Subproject commit fd0e7e0b0cf50131efaf536a5683266cfe169455 diff --git a/src/bro.bif b/src/bro.bif index 8ddde6ef86..1b1c23950d 100644 --- a/src/bro.bif +++ b/src/bro.bif @@ -11,6 +11,7 @@ #include #include #include +#include #include "digest.h" #include "Reporter.h" @@ -2615,15 +2616,15 @@ function to_double%(str: string%): double %{ const char* s = str->CheckString(); char* end_s; - + double d = strtod(s, &end_s); - + if ( s[0] == '\0' || end_s[0] != '\0' ) - { + { builtin_error("bad conversion to double", @ARG@[0]); d = 0; - } - + } + return new Val(d, TYPE_DOUBLE); %} @@ -3285,6 +3286,31 @@ function strftime%(fmt: string, d: time%) : string return new StringVal(buffer); %} + +## Parse a textual representation of a date/time value into a ``time`` type value. +## +## fmt: The format string used to parse the following *d* argument. See ``man strftime`` +## for the syntax. +## +## d: The string representing the time. +## +## Returns: The time value calculated from parsing *d* with *fmt*. +function strptime%(fmt: string, d: string%) : time + %{ + const time_t timeval = time_t(NULL); + struct tm t = *localtime(&timeval); + + if ( strptime(d->CheckString(), fmt->CheckString(), &t) == NULL ) + { + reporter->Warning("strptime conversion failed: fmt:%s d:%s", fmt->CheckString(), d->CheckString()); + return new Val(0.0, TYPE_TIME); + } + + double ret = mktime(&t); + return new Val(ret, TYPE_TIME); + %} + + # =========================================================================== # # Network Type Processing diff --git a/testing/btest/Baseline/bifs.strptime/.stdout b/testing/btest/Baseline/bifs.strptime/.stdout new file mode 100644 index 0000000000..179612d4c4 --- /dev/null +++ b/testing/btest/Baseline/bifs.strptime/.stdout @@ -0,0 +1,2 @@ +1350604800.0 +0.0 diff --git a/testing/btest/Baseline/bifs.strptime/reporter.log b/testing/btest/Baseline/bifs.strptime/reporter.log new file mode 100644 index 0000000000..367dbd63c1 --- /dev/null +++ b/testing/btest/Baseline/bifs.strptime/reporter.log @@ -0,0 +1,10 @@ +#separator \x09 +#set_separator , +#empty_field (empty) +#unset_field - +#path reporter +#open 2012-10-19-06-06-36 +#fields ts level message location +#types time enum string string +0.000000 Reporter::WARNING strptime conversion failed: fmt:%m d:1980-10-24 (empty) +#close 2012-10-19-06-06-36 diff --git a/testing/btest/bifs/strptime.bro b/testing/btest/bifs/strptime.bro new file mode 100644 index 0000000000..7a58989679 --- /dev/null +++ b/testing/btest/bifs/strptime.bro @@ -0,0 +1,10 @@ +# +# @TEST-EXEC: bro %INPUT +# @TEST-EXEC: btest-diff .stdout +# @TEST-EXEC: btest-diff reporter.log + +event bro_init() + { + print strptime("%Y-%m-%d", "2012-10-19"); + print strptime("%m", "1980-10-24"); + } \ No newline at end of file