-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzTimeAddDuration.go
More file actions
59 lines (45 loc) · 1.12 KB
/
zTimeAddDuration.go
File metadata and controls
59 lines (45 loc) · 1.12 KB
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
52
53
54
55
56
57
58
59
package netroutine
import (
"context"
"encoding/json"
"time"
)
const idTimeAddDuration = "TimeAddDuration"
func init() {
blocks[idTimeAddDuration] = &TimeAddDuration{}
}
type TimeAddDuration struct {
ToKey string
TimeKey string
DurationKey string
}
func (b *TimeAddDuration) toBytes() ([]byte, error) {
return json.Marshal(b)
}
func (b *TimeAddDuration) fromBytes(bytes []byte) error {
return json.Unmarshal(bytes, b)
}
func (b *TimeAddDuration) kind() string {
return idTimeAddDuration
}
func (b *TimeAddDuration) Run(ctx context.Context, wce *Environment) (string, Status) {
t, ok := wce.getData(b.TimeKey)
if !ok {
return log(b, missingWorkingData(b.TimeKey), Error)
}
oTime, err := toTime(t)
if err != nil {
return log(b, reportError("to time", err), Error)
}
d, ok := wce.getData(b.DurationKey)
if !ok {
return log(b, missingWorkingData(b.DurationKey), Error)
}
oDuration, ok := d.(time.Duration)
if !ok {
return log(b, reportWrongType(b.DurationKey), Error)
}
nTime := oTime.Add(oDuration)
wce.setData(b.ToKey, nTime)
return log(b, setWorkingData(b.ToKey, nTime.String()), Success)
}