-
Notifications
You must be signed in to change notification settings - Fork 152
/
durations.go
51 lines (45 loc) · 1.41 KB
/
durations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package experimental
import (
"context"
"fmt"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const (
addDurationTo = "addDuration"
subtractDurationFrom = "subDuration"
)
func init() {
runtime.RegisterPackageValue("experimental", addDurationTo, addDuration(addDurationTo))
runtime.RegisterPackageValue("experimental", subtractDurationFrom, subDuration(subtractDurationFrom))
}
func addDuration(name string) values.Value {
tp := runtime.MustLookupBuiltinType("experimental", "addDuration")
fn := func(ctx context.Context, args values.Object) (values.Value, error) {
d, ok := args.Get("d")
if !ok {
return nil, fmt.Errorf("%s requires 'd' parameter", name)
}
t, ok := args.Get("to")
if !ok {
return nil, fmt.Errorf("%s requires 'to' parameter", name)
}
return values.NewTime(t.Time().Add(d.Duration())), nil
}
return values.NewFunction(name, tp, fn, false)
}
func subDuration(name string) values.Value {
tp := runtime.MustLookupBuiltinType("experimental", "subDuration")
fn := func(ctx context.Context, args values.Object) (values.Value, error) {
d, ok := args.Get("d")
if !ok {
return nil, fmt.Errorf("%s requires 'd' parameter", name)
}
t, ok := args.Get("from")
if !ok {
return nil, fmt.Errorf("%s requires 'from' parameter", name)
}
return values.NewTime(t.Time().Add(d.Duration().Mul(-1))), nil
}
return values.NewFunction(name, tp, fn, false)
}