-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnow.go
112 lines (89 loc) · 1.69 KB
/
now.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package hype
import (
"context"
"encoding/json"
"time"
)
var _ ExecutableNode = &Now{}
type Now struct {
*Element
}
func (now *Now) MarshalJSON() ([]byte, error) {
if now == nil {
return nil, ErrIsNil("now")
}
m, err := now.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(now)
return json.MarshalIndent(m, "", " ")
}
func (now *Now) Execute(ctx context.Context, doc *Document) error {
if now == nil {
return now.WrapErr(ErrIsNil("now"))
}
now.Lock()
defer now.Unlock()
if doc == nil {
return now.WrapErr(ErrIsNil("doc"))
}
p := doc.Parser
tm := p.Now()
fmt, _ := now.Get("gofmt")
s := now.format(tm, fmt)
now.Nodes = Nodes{Text(s)}
return nil
}
func (now *Now) format(tm time.Time, fmt string) string {
switch fmt {
case "Layout":
fmt = time.Layout
case "ANSIC":
fmt = time.ANSIC
case "UnixDate":
fmt = time.UnixDate
case "RubyDate":
fmt = time.RubyDate
case "RFC822":
fmt = time.RFC822
case "RFC822Z":
fmt = time.RFC822Z
case "RFC850":
fmt = time.RFC850
case "RFC1123":
fmt = time.RFC1123
case "RFC1123Z":
fmt = time.RFC1123Z
case "RFC3339":
fmt = time.RFC3339
case "RFC3339Nano":
fmt = time.RFC3339Nano
case "Kitchen":
fmt = time.Kitchen
case "Stamp":
fmt = time.Stamp
case "StampMilli":
fmt = time.StampMilli
case "StampMicro":
fmt = time.StampMicro
case "StampNano":
fmt = time.StampNano
}
if len(fmt) == 0 {
fmt = TIME_FORMAT
}
return tm.Format(fmt)
}
func NewNowNodes(p *Parser, el *Element) (Nodes, error) {
if p == nil {
return nil, el.WrapErr(ErrIsNil("parser"))
}
if el == nil {
return nil, el.WrapErr(ErrIsNil("element"))
}
node := &Now{
Element: el,
}
return Nodes{node}, nil
}