Processing of custom date layouts
To support arbitrary date layout parsing, one approach would be to define a custom "parsetime" function, which would take a date/time layout as a parameter, with the same format as time.Parse():
functions := map[string]govaluate.ExpressionFunction{
"parsetime": func(args ...interface{}) (interface{}, error) {
return time.Parse(args[0].(string), args[1].(string))
},
}
With the above function, it would be possible to evaluate the following expression:
parsetime('Mon Jan 2 15:04:05 2006', [ts])
where "ts" is a parameter. However, since govaluate always attempts to parse strings as dates, the layout (e.g 'Mon Jan 2 15:04:05 2006') would end up being parsed as a date.
I think it would be useful to provide a method to disable automatic date parsing, this may actually provide a small performance improvement as a side effect. I can work on a PR if that makes sense.
I agree, I've been a little uncomfortable with the automatic-parsing. It treats literal strings differently than parameter strings, and while the auto-parsing is useful in dead-simple expressions (such as foo > '1/1/1990'), sometimes it will creep up and surprise users with cases like this.
The way given above would work (toggling automatic parsing). However, a way i might prefer is to simply let users pass in a function pointer to NewEvaluableExpression that attempts the parsing the same way the library does (essentially making the call to tryParseTime a function pointer with a default value). It would necessitate changing some parser arguments to allow such a thing, but would let users automatically parse whatever they want however they want (including, for example, timestamps expressed in terms of martian sols rather than earth days, or something crazy like that).
However, that still leaves users surprised by the inconsistent behavior between string literals being auto-parsed, and parameters which are left alone. I could see that being addressed in a second change, but i'm not totally sure what it would look like - be it some kind of syntax or a bool value in the expression or what.