(n *expr.FuncNode)
| 367 | // Type time |
| 368 | func (m *ToDateIn) Type() value.ValueType { return value.TimeType } |
| 369 | func (m *ToDateIn) Validate(n *expr.FuncNode) (expr.EvaluatorFunc, error) { |
| 370 | if len(n.Args) != 2 { |
| 371 | return nil, fmt.Errorf(`Expected args for todatein( (field | "now-3h" ), location) but got %s`, n) |
| 372 | } |
| 373 | |
| 374 | sn, ok := n.Args[1].(*expr.StringNode) |
| 375 | if !ok { |
| 376 | return nil, fmt.Errorf("Expected a string literal value for location like America/Los_Angeles") |
| 377 | } |
| 378 | |
| 379 | loc, err := time.LoadLocation(sn.Text) |
| 380 | if err != nil { |
| 381 | return nil, err |
| 382 | } |
| 383 | |
| 384 | if sn, ok = n.Args[0].(*expr.StringNode); ok { |
| 385 | dateStr := sn.Text |
| 386 | if ok && len(dateStr) > 3 && strings.ToLower(dateStr[:3]) == "now" { |
| 387 | _, err = datemath.Eval(dateStr) |
| 388 | if err != nil { |
| 389 | return nil, fmt.Errorf("%q was not able to be parsed %v", dateStr, err) |
| 390 | } |
| 391 | // Is date math |
| 392 | return func(_ expr.EvalContext, _ []value.Value) (value.Value, bool) { |
| 393 | t, _ := datemath.Eval(dateStr) |
| 394 | return value.NewTimeValue(t), true |
| 395 | }, nil |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Return the Evaluator |
| 400 | return func(_ expr.EvalContext, args []value.Value) (value.Value, bool) { |
| 401 | valueDateStr, ok := value.ValueToString(args[0]) |
| 402 | if !ok { |
| 403 | return value.TimeZeroValue, false |
| 404 | } |
| 405 | if t, err := dateparse.ParseIn(valueDateStr, loc); err == nil { |
| 406 | // We are going to correct back to UTC. so all fields are in UTC. |
| 407 | return value.NewTimeValue(t.In(time.UTC)), true |
| 408 | } |
| 409 | return value.TimeZeroValue, false |
| 410 | }, nil |
| 411 | } |
| 412 | |
| 413 | // TimeSeconds time in Seconds, parses a variety of formats looking for seconds |
| 414 | // See github.com/araddon/dateparse for formats supported on date parsing |
nothing calls this directly
no test coverage detected