Add SQL function time.week()

This commit is contained in:
Tobie Morgan Hitchcock 2018-09-19 14:46:05 +01:00
parent 918c696eb1
commit 391fb53743
4 changed files with 36 additions and 0 deletions

View file

@ -193,6 +193,7 @@ 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.week": {0: nil, 1: nil},
"time.year": {0: nil, 1: nil},
// Url implementation

View file

@ -248,6 +248,8 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er
return timeSecs(ctx, args...)
case "time.unix":
return timeUnix(ctx, args...)
case "time.week":
return timeWeek(ctx, args...)
case "time.year":
return timeYear(ctx, args...)

View file

@ -143,6 +143,20 @@ func timeUnix(ctx context.Context, args ...interface{}) (interface{}, error) {
return nil, nil
}
func timeWeek(ctx context.Context, args ...interface{}) (interface{}, error) {
switch len(args) {
case 0:
_, w := time.Now().ISOWeek()
return float64(w), nil
case 1:
if v, ok := ensureTime(args[0]); ok {
_, w := v.ISOWeek()
return float64(w), nil
}
}
return nil, nil
}
func timeYear(ctx context.Context, args ...interface{}) (interface{}, error) {
switch len(args) {
case 0:

View file

@ -213,6 +213,25 @@ func TestTime(t *testing.T) {
So(res, ShouldEqual, nil)
})
Convey("time.week() works properly", t, func() {
res, _ := Run(context.Background(), "time.week")
So(res, ShouldHaveSameTypeAs, float64(0))
_, val := now.ISOWeek()
So(res, ShouldBeGreaterThanOrEqualTo, val)
})
Convey("time.week(a) works properly", t, func() {
res, _ := Run(context.Background(), "time.week", old)
So(res, ShouldHaveSameTypeAs, float64(0))
So(res, ShouldEqual, 1)
})
Convey("time.week(a,b,c) errors properly", t, func() {
res, _ := Run(context.Background(), "time.week", "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))