From d157759ff2f7741d116ebbb3add637ef6bce5163 Mon Sep 17 00:00:00 2001 From: Seth Hall Date: Fri, 19 Oct 2012 02:07:34 -0400 Subject: [PATCH] Added a BiF to wrap the strptime function. --- src/bro.bif | 25 +++++++++++++++++++ testing/btest/Baseline/bifs.strptime/.stdout | 2 ++ .../btest/Baseline/bifs.strptime/reporter.log | 10 ++++++++ testing/btest/bifs/strptime.bro | 10 ++++++++ 4 files changed, 47 insertions(+) create mode 100644 testing/btest/Baseline/bifs.strptime/.stdout create mode 100644 testing/btest/Baseline/bifs.strptime/reporter.log create mode 100644 testing/btest/bifs/strptime.bro diff --git a/src/bro.bif b/src/bro.bif index 8ddde6ef86..d59ae36b28 100644 --- a/src/bro.bif +++ b/src/bro.bif @@ -11,6 +11,7 @@ #include #include #include +#include #include "digest.h" #include "Reporter.h" @@ -3285,6 +3286,30 @@ 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((double) 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