3
0

Add date_format to supported QL functions

This commit is contained in:
Tomaž Jerman
2022-09-14 19:23:04 +02:00
parent 65e6976855
commit 9fdb86c4cf
4 changed files with 30 additions and 19 deletions
+6
View File
@@ -166,6 +166,11 @@ var (
return fmt.Sprintf("date(%s)", args[0])
},
},
"date_format": {
Handler: func(args ...string) string {
return fmt.Sprintf("strftime(%s, %s)", args[0], args[1])
},
},
// generic stuff
"null": {
@@ -270,6 +275,7 @@ func newGval(expr string) (gval.Evaluable, error) {
gval.Function("quarter", gvalfnc.Quarter),
gval.Function("year", gvalfnc.Year),
gval.Function("month", gvalfnc.Month),
gval.Function("strftime", gvalfnc.StrfTime),
gval.Function("date", gvalfnc.Date),
gval.Function("isNil", gvalfnc.IsNil),
gval.Function("float", gvalfnc.CastFloat),
+1 -19
View File
@@ -6,7 +6,6 @@ import (
"github.com/PaesslerAG/gval"
"github.com/cortezaproject/corteza-server/pkg/gvalfnc"
"github.com/lestrrat-go/strftime"
)
func TimeFunctions() []gval.Language {
@@ -20,7 +19,7 @@ func TimeFunctions() []gval.Language {
gval.Function("modMonth", modMonth),
gval.Function("modYear", modYear),
gval.Function("parseDuration", time.ParseDuration),
gval.Function("strftime", strfTime),
gval.Function("strftime", gvalfnc.StrfTime),
gval.Function("isLeapYear", isLeapYear),
gval.Function("now", now),
gval.Function("isWeekDay", isWeekDay),
@@ -163,23 +162,6 @@ func modYear(base interface{}, mod interface{}) (*time.Time, error) {
return &tmp, nil
}
// Strftime formats time with POSIX standard format
// More details here:
// https://github.com/lestrrat-go/strftime#supported-conversion-specifications
func strfTime(base interface{}, f string) (string, error) {
t, _, err := gvalfnc.PrepMod(base, 0)
if err != nil {
return "", err
}
o, _ := strftime.Format(f, *t,
strftime.WithMilliseconds('b'),
strftime.WithUnixSeconds('L'))
return o, nil
}
// sub returns difference between two date into milliseconds
func sub(from interface{}, to interface{}) (out int64, err error) {
t1, _, err := gvalfnc.PrepMod(from, 0)
+18
View File
@@ -4,6 +4,7 @@ import (
"errors"
"time"
"github.com/lestrrat-go/strftime"
"github.com/spf13/cast"
)
@@ -76,3 +77,20 @@ func PrepMod(base interface{}, mod interface{}) (*time.Time, int, error) {
return t, m, nil
}
// Strftime formats time with POSIX standard format
// More details here:
// https://github.com/lestrrat-go/strftime#supported-conversion-specifications
func StrfTime(base interface{}, f string) (string, error) {
t, _, err := PrepMod(base, 0)
if err != nil {
return "", err
}
o, _ := strftime.Format(f, *t,
strftime.WithMilliseconds('b'),
strftime.WithUnixSeconds('L'))
return o, nil
}
+5
View File
@@ -176,6 +176,11 @@ var (
//},
// - filtering
"date_format": {
Handler: func(args ...exp.Expression) exp.Expression {
return exp.NewSQLFunctionExpression("DATE_FORMAT", args[0], args[1])
},
},
"now": {
Handler: func(args ...exp.Expression) exp.Expression {
return exp.NewSQLFunctionExpression("NOW")