From 480ae0404622e280f0e8fc93d0148310059ac503 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 20 Sep 2018 12:55:45 +0100 Subject: [PATCH] Add SQL functions time.wday() and time.yday() --- sql/funcs.go | 2 ++ util/fncs/fnc.go | 4 ++++ util/fncs/time.go | 24 ++++++++++++++++++++++++ util/fncs/time_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/sql/funcs.go b/sql/funcs.go index b612dd96..b77beb0c 100644 --- a/sql/funcs.go +++ b/sql/funcs.go @@ -193,7 +193,9 @@ var funcs = map[string]map[int]interface{}{ "time.nano": {0: nil, 1: nil}, "time.secs": {0: nil, 1: nil}, "time.unix": {0: nil, 1: nil}, + "time.wday": {0: nil, 1: nil}, "time.week": {0: nil, 1: nil}, + "time.yday": {0: nil, 1: nil}, "time.year": {0: nil, 1: nil}, // Url implementation diff --git a/util/fncs/fnc.go b/util/fncs/fnc.go index c984ec86..80c233b1 100644 --- a/util/fncs/fnc.go +++ b/util/fncs/fnc.go @@ -248,8 +248,12 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er return timeSecs(ctx, args...) case "time.unix": return timeUnix(ctx, args...) + case "time.wday": + return timeWday(ctx, args...) case "time.week": return timeWeek(ctx, args...) + case "time.yday": + return timeYday(ctx, args...) case "time.year": return timeYear(ctx, args...) diff --git a/util/fncs/time.go b/util/fncs/time.go index d2008704..1dd27265 100644 --- a/util/fncs/time.go +++ b/util/fncs/time.go @@ -143,6 +143,18 @@ func timeUnix(ctx context.Context, args ...interface{}) (interface{}, error) { return nil, nil } +func timeWday(ctx context.Context, args ...interface{}) (interface{}, error) { + switch len(args) { + case 0: + return float64(time.Now().Weekday()), nil + case 1: + if v, ok := ensureTime(args[0]); ok { + return float64(v.Weekday()), nil + } + } + return nil, nil +} + func timeWeek(ctx context.Context, args ...interface{}) (interface{}, error) { switch len(args) { case 0: @@ -157,6 +169,18 @@ func timeWeek(ctx context.Context, args ...interface{}) (interface{}, error) { return nil, nil } +func timeYday(ctx context.Context, args ...interface{}) (interface{}, error) { + switch len(args) { + case 0: + return float64(time.Now().YearDay()), nil + case 1: + if v, ok := ensureTime(args[0]); ok { + return float64(v.YearDay()), nil + } + } + return nil, nil +} + func timeYear(ctx context.Context, args ...interface{}) (interface{}, error) { switch len(args) { case 0: diff --git a/util/fncs/time_test.go b/util/fncs/time_test.go index caa21754..849ce9f0 100644 --- a/util/fncs/time_test.go +++ b/util/fncs/time_test.go @@ -213,6 +213,24 @@ func TestTime(t *testing.T) { So(res, ShouldEqual, nil) }) + Convey("time.wday() works properly", t, func() { + res, _ := Run(context.Background(), "time.wday") + So(res, ShouldHaveSameTypeAs, float64(0)) + So(res, ShouldBeGreaterThanOrEqualTo, now.Weekday()) + }) + + Convey("time.wday(a) works properly", t, func() { + res, _ := Run(context.Background(), "time.wday", old) + So(res, ShouldHaveSameTypeAs, float64(0)) + So(res, ShouldEqual, 1) + }) + + Convey("time.wday(a,b,c) errors properly", t, func() { + res, _ := Run(context.Background(), "time.wday", "one", "two") + So(res, ShouldHaveSameTypeAs, nil) + So(res, ShouldEqual, nil) + }) + Convey("time.week() works properly", t, func() { res, _ := Run(context.Background(), "time.week") So(res, ShouldHaveSameTypeAs, float64(0)) @@ -232,6 +250,24 @@ func TestTime(t *testing.T) { So(res, ShouldEqual, nil) }) + Convey("time.yday() works properly", t, func() { + res, _ := Run(context.Background(), "time.yday") + So(res, ShouldHaveSameTypeAs, float64(0)) + So(res, ShouldEqual, now.YearDay()) + }) + + Convey("time.yday(a) works properly", t, func() { + res, _ := Run(context.Background(), "time.yday", old) + So(res, ShouldHaveSameTypeAs, float64(0)) + So(res, ShouldEqual, 2) + }) + + Convey("time.yday(a,b,c) errors properly", t, func() { + res, _ := Run(context.Background(), "time.yday", "one", "two") + So(res, ShouldHaveSameTypeAs, nil) + So(res, ShouldEqual, nil) + }) + Convey("time.year() works properly", t, func() { res, _ := Run(context.Background(), "time.year") So(res, ShouldHaveSameTypeAs, float64(0))