snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
186
server/pkg/expr/func_time.go
Normal file
186
server/pkg/expr/func_time.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/PaesslerAG/gval"
|
||||
"github.com/cortezaproject/corteza/server/pkg/gvalfnc"
|
||||
)
|
||||
|
||||
func TimeFunctions() []gval.Language {
|
||||
return []gval.Language{
|
||||
gval.Function("earliest", earliest),
|
||||
gval.Function("latest", latest),
|
||||
gval.Function("parseISOTime", parseISOTime),
|
||||
gval.Function("modTime", modTime),
|
||||
gval.Function("modDate", modDate),
|
||||
gval.Function("modWeek", modWeek),
|
||||
gval.Function("modMonth", modMonth),
|
||||
gval.Function("modYear", modYear),
|
||||
gval.Function("parseDuration", time.ParseDuration),
|
||||
gval.Function("strftime", gvalfnc.StrfTime),
|
||||
gval.Function("isLeapYear", isLeapYear),
|
||||
gval.Function("now", now),
|
||||
gval.Function("isWeekDay", isWeekDay),
|
||||
gval.Function("sub", sub),
|
||||
}
|
||||
}
|
||||
|
||||
func now() time.Time {
|
||||
return gvalfnc.Now()
|
||||
}
|
||||
|
||||
func isLeapYear(base interface{}) (bool, error) {
|
||||
t, _, err := gvalfnc.PrepMod(base, 0)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return time.Date(t.Year(), time.December, 31, 0, 0, 0, 0, time.Local).YearDay() == 366, nil
|
||||
}
|
||||
|
||||
func isLeapDay(base interface{}) (bool, error) {
|
||||
t, _, err := gvalfnc.PrepMod(base, 0)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return t.Day() == 29 && t.Month() == 2, nil
|
||||
}
|
||||
|
||||
func isWeekDay(base interface{}) (bool, error) {
|
||||
t, _, err := gvalfnc.PrepMod(base, 0)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return time.Sunday < t.Weekday() && t.Weekday() < time.Saturday, nil
|
||||
}
|
||||
|
||||
func earliest(f interface{}, aa ...interface{}) (*time.Time, error) {
|
||||
t, _, err := gvalfnc.PrepMod(f, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range aa {
|
||||
s, _, err := gvalfnc.PrepMod(a, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if (*s).Before(*t) {
|
||||
t = s
|
||||
}
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func latest(f interface{}, aa ...interface{}) (*time.Time, error) {
|
||||
t, _, err := gvalfnc.PrepMod(f, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range aa {
|
||||
s, _, err := gvalfnc.PrepMod(a, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.After(*t) {
|
||||
t = s
|
||||
}
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func parseISOTime(s string) (time.Time, error) {
|
||||
return time.Parse(time.RFC3339, s)
|
||||
}
|
||||
|
||||
func modTime(base interface{}, mod interface{}) (*time.Time, error) {
|
||||
var (
|
||||
err error
|
||||
d time.Duration
|
||||
t *time.Time
|
||||
)
|
||||
|
||||
t, _, err = gvalfnc.PrepMod(base, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch c := mod.(type) {
|
||||
case time.Duration:
|
||||
d = c
|
||||
case string:
|
||||
d, err = time.ParseDuration(c)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
|
||||
tmp := t.Add(d)
|
||||
return &tmp, nil
|
||||
}
|
||||
|
||||
func modDate(base interface{}, mod interface{}) (*time.Time, error) {
|
||||
t, m, err := gvalfnc.PrepMod(base, mod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmp := t.AddDate(0, 0, m)
|
||||
return &tmp, nil
|
||||
}
|
||||
|
||||
func modWeek(base interface{}, mod interface{}) (*time.Time, error) {
|
||||
t, m, err := gvalfnc.PrepMod(base, mod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmp := t.AddDate(0, 0, 7*m)
|
||||
return &tmp, nil
|
||||
}
|
||||
|
||||
func modMonth(base interface{}, mod interface{}) (*time.Time, error) {
|
||||
t, m, err := gvalfnc.PrepMod(base, mod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmp := t.AddDate(0, m, 0)
|
||||
return &tmp, nil
|
||||
}
|
||||
|
||||
func modYear(base interface{}, mod interface{}) (*time.Time, error) {
|
||||
t, m, err := gvalfnc.PrepMod(base, mod)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tmp := t.AddDate(m, 0, 0)
|
||||
return &tmp, 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)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t2, _, err := gvalfnc.PrepMod(to, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var duration time.Duration
|
||||
|
||||
if t1.After(*t2) {
|
||||
duration = t1.Sub(*t2)
|
||||
} else {
|
||||
return -1, fmt.Errorf("expecting 2nd input to be less than 1st input")
|
||||
}
|
||||
|
||||
return duration.Milliseconds(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user