Add SQL functions time.wday() and time.yday()

This commit is contained in:
Tobie Morgan Hitchcock 2018-09-20 12:55:45 +01:00
parent 391fb53743
commit 480ae04046
4 changed files with 66 additions and 0 deletions

View file

@ -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

View file

@ -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...)

View file

@ -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:

View file

@ -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))