-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
421 lines (415 loc) · 13.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package builtin_libs
import (
"regexp"
"time"
. "github.com/zgg-lang/zgg-go/runtime"
)
var timeClass ValueType
func libTime(*Context) ValueObject {
lib := NewObject()
lib.SetMember("Time", timeClass, nil)
lib.SetMember("time", NewNativeFunction("time", func(c *Context, this Value, args []Value) Value {
return NewInt(time.Now().Unix())
}), nil)
lib.SetMember("now", NewNativeFunction("now", func(c *Context, this Value, args []Value) Value {
var timeType ValueStr
EnsureFuncParams(c, "now", args, ArgRuleOptional{"timeType", TypeStr, &timeType, NewStr("")})
nowTs := time.Now().UnixNano()
asType := timeType.Value()
mod := int64(1)
switch asType {
case "day":
mod = 86400 * 1e9
case "hour":
mod = 3600 * 1e9
case "minute":
mod = 60 * 1e9
case "second":
mod = 1e9
case "":
default:
c.RaiseRuntimeError("Invalid time type %s", asType)
}
now := NewObjectAndInit(timeClass, c, NewInt(nowTs-nowTs%mod))
if asType != "" {
now.SetMember("__as", timeType, c)
}
return now
}), nil)
lib.SetMember("fromUnix", NewNativeFunction("fromUnix", func(c *Context, this Value, args []Value) Value {
var ts ValueInt
EnsureFuncParams(c, "time.fromUnix", args, ArgRuleRequired{"unixTimestamp", TypeInt, &ts})
return NewObjectAndInit(timeClass, c, NewInt(ts.Value()*1e9))
}, "timestamp"), nil)
lib.SetMember("fromGoTime", NewNativeFunction("fromGoTime", func(c *Context, this Value, args []Value) Value {
var gt GoValue
EnsureFuncParams(c, "time.fromGoTime", args, ArgRuleRequired{"time", TypeGoValue, >})
if _, ok := gt.ToGoValue().(time.Time); !ok {
c.RaiseRuntimeError("Not a time.Time!")
}
return NewObjectAndInit(timeClass, c, gt)
}, "time"), nil)
lib.SetMember("sleep", NewNativeFunction("time", func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.RaiseRuntimeError("sleep: requires 1 argument")
return nil
}
sleepSeconds := c.MustFloat(args[0])
time.Sleep(time.Duration(sleepSeconds * float64(time.Second)))
return Undefined()
}), nil)
lib.SetMember("timeit", NewNativeFunction("timeit", func(c *Context, this Value, args []Value) (rv Value) {
var callable ValueCallable
EnsureFuncParams(c, "timeit", args,
ArgRuleRequired{"callable", TypeCallable, &callable},
)
t0 := time.Now()
defer func() {
rv = NewInt(time.Now().UnixNano() - t0.UnixNano())
}()
c.Invoke(callable, nil, NoArgs)
return
}), nil)
return lib
}
func initTimeClass() {
layoutP := regexp.MustCompile("%.")
timeClass = NewClassBuilder("Time").
Constructor(func(c *Context, thisObj ValueObject, args []Value) {
// var ts int64
var _t time.Time
var as string
switch len(args) {
case 0:
_t = time.Now()
case 1:
switch v := args[0].(type) {
case ValueInt:
ts := v.Value()
_t = time.Unix(ts/1e9, ts%1e9)
case GoValue:
switch gv := v.ToGoValue().(type) {
case time.Time:
_t = gv
default:
c.RaiseRuntimeError("Time.__init__: invalid arg %v", gv)
}
case ValueStr:
{
var layout string
switch v.Len() {
case 8:
layout = "20060102"
as = "day"
case 10:
layout = "2006-01-02"
as = "day"
case 14:
layout = "20060102150405"
as = "second"
case 19:
layout = "2006-01-02 15:04:05"
as = "second"
default:
c.RaiseRuntimeError("Time.__init__: invalid time str %s", v.Value())
}
t, err := time.Parse(layout, v.Value())
if err != nil {
c.RaiseRuntimeError("Time.__init__: parse time error %s", err)
}
// ts = t.UnixNano()
_t = t
}
}
case 2:
{
var timeStr, layout ValueStr
EnsureFuncParams(c, "Time.__init__", args,
ArgRuleRequired{"timeStr", TypeStr, &timeStr},
ArgRuleRequired{"layout", TypeStr, &layout},
)
t, err := time.Parse(layout.Value(), timeStr.Value())
if err != nil {
c.RaiseRuntimeError("Time.__init__: parse time error %s", err)
}
_t = t
}
case 3:
{
var year, month, day ValueInt
EnsureFuncParams(c, "Time.__init__", args,
ArgRuleRequired{"year", TypeInt, &year},
ArgRuleRequired{"month", TypeInt, &month},
ArgRuleRequired{"day", TypeInt, &day},
)
_t = time.Date(year.AsInt(), time.Month(month.AsInt()), day.AsInt(), 0, 0, 0, 0, time.Local)
}
case 6:
{
var year, month, day, hour, minute, second ValueInt
EnsureFuncParams(c, "Time.__init__", args,
ArgRuleRequired{"year", TypeInt, &year},
ArgRuleRequired{"month", TypeInt, &month},
ArgRuleRequired{"day", TypeInt, &day},
ArgRuleRequired{"hour", TypeInt, &hour},
ArgRuleRequired{"minute", TypeInt, &minute},
ArgRuleRequired{"second", TypeInt, &second},
)
_t = time.Date(
year.AsInt(),
time.Month(month.AsInt()),
day.AsInt(),
hour.AsInt(),
minute.AsInt(),
second.AsInt(),
0,
time.Local,
)
}
default:
c.RaiseRuntimeError("Time.__init__: invalid args")
}
thisObj.SetMember("__t", NewGoValue(_t), c)
if as != "" {
thisObj.SetMember("__as", NewStr(as), c)
}
}).
Method("__getAttr__", func(c *Context, this ValueObject, args []Value) Value {
var field ValueStr
EnsureFuncParams(c, "Time.__getAttr__", args, ArgRuleRequired{"field", TypeStr, &field})
t := this.GetMember("__t", c).ToGoValue().(time.Time)
switch field.Value() {
case "unix":
return NewInt(t.Unix())
case "unixNano":
return NewInt(t.UnixNano())
case "year":
return NewInt(int64(t.Year()))
case "month":
return NewInt(int64(t.Month()))
case "day":
return NewInt(int64(t.Day()))
case "hour":
return NewInt(int64(t.Hour()))
case "minute":
return NewInt(int64(t.Minute()))
case "second":
return NewInt(int64(t.Second()))
}
return Undefined()
}).
Method("add", func(c *Context, this ValueObject, args []Value) Value {
var duration ValueStr
EnsureFuncParams(c, "Time.add", args, ArgRuleRequired{"duration", TypeStr, &duration})
d, err := time.ParseDuration(duration.Value())
if err != nil {
c.RaiseRuntimeError("Invalid duration %s", duration.Value())
}
t := this.GetMember("__t", c).ToGoValue().(time.Time)
rt := t.Add(d)
rv := NewObjectAndInit(timeClass, c, NewGoValue(rt))
rv.SetMember("__as", this.GetMember("__as", c), c)
return rv
}).
Method("addDays", func(c *Context, this ValueObject, args []Value) Value {
var days ValueInt
EnsureFuncParams(c, "Time.addDays", args,
ArgRuleRequired{"days", TypeInt, &days},
)
t := this.GetMember("__t", c).ToGoValue().(time.Time)
rv := NewObjectAndInit(timeClass, c, NewGoValue(t.AddDate(0, 0, days.AsInt())))
rv.SetMember("__as", this.GetMember("__as", c), c)
return rv
}).
Method("addHours", func(c *Context, this ValueObject, args []Value) Value {
var hours ValueInt
EnsureFuncParams(c, "Time.addHours", args,
ArgRuleRequired{"days", TypeInt, &hours},
)
t := this.GetMember("__t", c).ToGoValue().(time.Time)
rv := NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Duration(hours.AsInt())*time.Hour)))
rv.SetMember("__as", this.GetMember("__as", c), c)
return rv
}).
Method("as", func(c *Context, this ValueObject, args []Value) Value {
var asType ValueStr
EnsureFuncParams(c, "Time.as", args, ArgRuleRequired{"asType", TypeStr, &asType})
this.SetMember("__as", asType, c)
return this
}).
Method("__next__", func(c *Context, this ValueObject, args []Value) Value {
if as, ok := this.GetMember("__as", c).(ValueStr); ok {
var r Value
t := this.GetMember("__t", c).ToGoValue().(time.Time)
switch as.Value() {
case "day":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.AddDate(0, 0, 1)))
case "hour":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Hour)))
case "minute":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Minute)))
case "second":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Second)))
}
if r != nil {
r.(ValueObject).SetMember("__as", as, c)
return r
}
}
c.RaiseRuntimeError("Time object cannot get __next__ without specialized time type")
return nil
}).
Method("format", func(c *Context, this ValueObject, args []Value) Value {
var (
layout ValueStr
timezone ValueStr
)
EnsureFuncParams(c, "Time.format", args,
ArgRuleRequired{"layout", TypeStr, &layout},
ArgRuleOptional{"timezone", TypeStr, &timezone, NewStr("")},
)
t := this.GetMember("__t", c).ToGoValue().(time.Time)
layoutStr := layoutP.ReplaceAllStringFunc(layout.Value(), func(s string) string {
switch s {
case "%Y":
return "2006"
case "%y":
return "06"
case "%m":
return "01"
case "%d":
return "02"
case "%H":
return "15"
case "%M":
return "04"
case "%S":
return "05"
}
return s
})
if tz := timezone.Value(); tz != "" {
loc, err := time.LoadLocation(tz)
if err != nil {
c.RaiseRuntimeError("Invalid timezone %s", tz)
}
t = t.In(loc)
}
return NewStr(t.Format(layoutStr))
}).
Method("__add__", func(c *Context, this ValueObject, args []Value) Value {
var diff ValueInt
EnsureFuncParams(c, "Time.__add__", args, ArgRuleRequired{"add", TypeInt, &diff})
if as, ok := this.GetMember("__as", c).(ValueStr); ok {
var r Value
t := this.GetMember("__t", c).ToGoValue().(time.Time)
d := diff.AsInt()
switch as.Value() {
case "day":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.AddDate(0, 0, d)))
case "hour":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Hour*time.Duration(d))))
case "minute":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Minute*time.Duration(d))))
case "second":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Second*time.Duration(d))))
}
if r != nil {
r.(ValueObject).SetMember("__as", as, c)
return r
}
}
c.RaiseRuntimeError("Time object cannot get __next__ without specialized time type")
return nil
}).
Method("__sub__", func(c *Context, this ValueObject, args []Value) Value {
var diff ValueInt
EnsureFuncParams(c, "Time.__sub__", args, ArgRuleRequired{"add", TypeInt, &diff})
if as, ok := this.GetMember("__as", c).(ValueStr); ok {
var r Value
t := this.GetMember("__t", c).ToGoValue().(time.Time)
d := -diff.AsInt()
switch as.Value() {
case "day":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.AddDate(0, 0, d)))
case "hour":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Hour*time.Duration(d))))
case "minute":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Minute*time.Duration(d))))
case "second":
r = NewObjectAndInit(timeClass, c, NewGoValue(t.Add(time.Second*time.Duration(d))))
}
if r != nil {
r.(ValueObject).SetMember("__as", as, c)
return r
}
}
c.RaiseRuntimeError("Time object cannot get __next__ without specialized time type")
return nil
}).
Method("timetuple", func(c *Context, this ValueObject, args []Value) Value {
t := this.GetMember("__t", c).ToGoValue().(time.Time)
return NewArrayByValues(
NewInt(int64(t.Year())),
NewInt(int64(t.Month())),
NewInt(int64(t.Day())),
NewInt(int64(t.Hour())),
NewInt(int64(t.Minute())),
NewInt(int64(t.Second())),
NewStr(t.Location().String()),
)
}).
Method("__str__", func(c *Context, this ValueObject, args []Value) Value {
t := this.GetMember("__t", c).ToGoValue().(time.Time)
if as, ok := this.GetMember("__as", c).(ValueStr); ok {
switch as.Value() {
case "day":
return NewStr(t.Format("2006-01-02"))
case "hour":
return NewStr(t.Format("2006-01-02 15"))
case "minute":
return NewStr(t.Format("2006-01-02 15:04"))
case "second":
return NewStr(t.Format("2006-01-02 15:04:05"))
}
}
return NewStr(t.Format("Time(2006-01-02 15:04:05)"))
}).
Method("__lt__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(t1.Before(t2))
}).
Method("__le__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(!t1.After(t2))
}).
Method("__gt__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(t1.After(t2))
}).
Method("__ge__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(!t1.Before(t2))
}).
Method("__eq__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(!t1.Before(t2) && !t1.After(t2))
}).
Method("__ne__", func(c *Context, this ValueObject, args []Value) Value {
t1 := this.GetMember("__t", c).ToGoValue().(time.Time)
t2 := args[0].GetMember("__t", c).ToGoValue().(time.Time)
return NewBool(t1.Before(t2) || t1.After(t2))
}).
Method("toGoTime", func(c *Context, this ValueObject, args []Value) Value {
return this.GetMember("__t", c)
}).
Build()
}
func init() {
initTimeClass()
}