-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
243 lines (218 loc) · 5.7 KB
/
time.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package lib
import (
"errors"
"fmt"
"time"
"github.com/dcaiafa/nitro"
)
type Time struct {
time time.Time
}
var _ /* implements */ nitro.Indexable = Time{}
func NewTime(t time.Time) Time {
return Time{time: t}
}
func (t Time) String() string { return t.time.String() }
func (t Time) Type() string { return "time" }
func (t Time) EvalOp(op nitro.Op, operand nitro.Value) (nitro.Value, error) {
switch op {
case nitro.OpEq, nitro.OpSub, nitro.OpLT, nitro.OpLE, nitro.OpGT, nitro.OpGE:
operandTime, ok := operand.(Time)
if !ok {
return nil, fmt.Errorf(
"invalid operation between time and %v",
nitro.TypeName(operand))
}
switch op {
case nitro.OpEq:
return nitro.NewBool(t.time.Equal(operandTime.time)), nil
case nitro.OpSub:
return Duration{t.time.Sub(operandTime.time)}, nil
case nitro.OpLT:
return nitro.NewBool(t.time.Before(operandTime.time)), nil
case nitro.OpLE:
return nitro.NewBool(t.time.Before(operandTime.time) ||
t.time.Equal(operandTime.time)), nil
case nitro.OpGT:
return nitro.NewBool(t.time.After(operandTime.time)), nil
case nitro.OpGE:
return nitro.NewBool(t.time.After(operandTime.time) ||
t.time.Equal(operandTime.time)), nil
default:
panic("unreachable")
}
case nitro.OpAdd:
operandDur, ok := operand.(Duration)
if !ok {
return nil, fmt.Errorf(
"invalid operation between time and %v",
nitro.TypeName(operand))
}
return Time{t.time.Add(operandDur.dur)}, nil
default:
return nil, fmt.Errorf("operation is not supported by time")
}
}
func (t Time) Index(key nitro.Value) (nitro.Value, error) {
keyStr, ok := key.(nitro.String)
if !ok {
return nil, fmt.Errorf(
"time cannot be indexed by %q",
nitro.TypeName(key))
}
switch keyStr.String() {
case "utc":
return nitro.NewNativeFn(t.utc), nil
case "local":
return nitro.NewNativeFn(t.local), nil
case "format":
return nitro.NewNativeFn(t.format), nil
case "unix":
return nitro.NewNativeFn(t.unix), nil
case "unixnano":
return nitro.NewNativeFn(t.unixnano), nil
case "year":
return nitro.NewInt(int64(t.time.Year())), nil
case "month":
return nitro.NewInt(int64(t.time.Month())), nil
case "day":
return nitro.NewInt(int64(t.time.Day())), nil
case "hour":
return nitro.NewInt(int64(t.time.Hour())), nil
case "minute":
return nitro.NewInt(int64(t.time.Minute())), nil
case "second":
return nitro.NewInt(int64(t.time.Second())), nil
case "nanosecond":
return nitro.NewInt(int64(t.time.Nanosecond())), nil
default:
return nil, fmt.Errorf(
"time does not have member %q",
keyStr.String())
}
}
func (t Time) IndexRef(key nitro.Value) (nitro.ValueRef, error) {
return nitro.ValueRef{}, fmt.Errorf("time is read-only")
}
func (t Time) utc(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 {
return nil, fmt.Errorf("utc takes no arguments")
}
t.time = t.time.UTC()
return []nitro.Value{t}, nil
}
func (t Time) local(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 {
return nil, fmt.Errorf("local takes no arguments")
}
t.time = t.time.Local()
return []nitro.Value{t}, nil
}
var errTimeFormatUsage = errors.New(
"invalid usage. Expected <time>.format(layout: string?)")
func (t Time) format(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 && len(args) != 1 {
return nil, errTimeFormatUsage
}
layout := time.RFC3339
if len(args) == 1 {
layoutArg, ok := args[0].(nitro.String)
if !ok {
return nil, errTimeFormatUsage
}
layout = convertTimeLayout(layoutArg.String())
}
res := t.time.Format(layout)
return []nitro.Value{nitro.NewString(res)}, nil
}
func (t Time) unix(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 {
return nil, errTakesNoArgs
}
return []nitro.Value{nitro.NewInt(t.time.Unix())}, nil
}
func (t Time) unixnano(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 {
return nil, errTakesNoArgs
}
return []nitro.Value{nitro.NewInt(t.time.UnixNano())}, nil
}
func now(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) != 0 {
return nil, errTakesNoArgs
}
return []nitro.Value{Time{time.Now()}}, nil
}
func parseTime(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
timeStr, err := getStringArg(args, 0)
if err != nil {
return nil, err
}
var layout string
if len(args) >= 2 {
layout, err = getStringArg(args, 1)
if err != nil {
return nil, err
}
layout = convertTimeLayout(layout)
} else {
layout = time.RFC3339
}
t, err := time.Parse(layout, timeStr)
if err != nil {
return nil, err
}
return []nitro.Value{Time{time: t}}, nil
}
func timeFromUnix(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
var err error
var sec, nano int64
sec, err = getIntArg(args, 0)
if err != nil {
return nil, err
}
if len(args) >= 2 {
nano, err = getIntArg(args, 1)
if err != nil {
return nil, err
}
}
t := time.Unix(sec, nano)
return []nitro.Value{Time{time: t}}, nil
}
func convertTimeLayout(timeFmt string) string {
switch timeFmt {
case "ansic":
return time.ANSIC
case "unixdate":
return time.UnixDate
case "rubydate":
return time.RubyDate
case "rfc822":
return time.RFC822
case "rfc822z":
return time.RFC822Z
case "rfc850":
return time.RFC850
case "rfc1123":
return time.RFC1123
case "rfc1123z":
return time.RFC1123Z
case "rfc3339":
return time.RFC3339
case "rfc3339nano":
return time.RFC3339Nano
case "kitchen":
return time.Kitchen
case "stamp":
return time.Stamp
case "stampmilli":
return time.StampMilli
case "stampmicro":
return time.StampMicro
case "stampnano":
return time.StampNano
default:
return timeFmt
}
}