From f4fd104afdd67c0bf0bbcd34d3524e0b4f91ae10 Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Fri, 19 May 2017 11:38:46 -0400 Subject: [PATCH] Add unixNano built in function --- tick/stateful/functions.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tick/stateful/functions.go b/tick/stateful/functions.go index 4bf1f3993..066f5288c 100644 --- a/tick/stateful/functions.go +++ b/tick/stateful/functions.go @@ -168,6 +168,7 @@ func init() { statelessFuncs["isPresent"] = isPresent{} // Time functions + statelessFuncs["unixNano"] = unixNano{} statelessFuncs["minute"] = minute{} statelessFuncs["hour"] = hour{} statelessFuncs["weekday"] = weekday{} @@ -1135,6 +1136,30 @@ func init() { timeFuncSignature[d] = ast.TInt } +type unixNano struct { +} + +func (unixNano) Reset() { +} + +// Return the nanosecond unix timestamp for the given time. +func (unixNano) Call(args ...interface{}) (v interface{}, err error) { + if len(args) != 1 { + return 0, errors.New("unixNano expects exactly one argument") + } + switch a := args[0].(type) { + case time.Time: + v = int64(a.UnixNano()) + default: + err = fmt.Errorf("cannot convert %T to time.Time", a) + } + return +} + +func (unixNano) Signature() map[Domain]ast.ValueType { + return timeFuncSignature +} + type minute struct { }