/
time.go
118 lines (85 loc) · 2.95 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
package modules
import (
"time"
"ghostlang.org/x/ghost/object"
"ghostlang.org/x/ghost/token"
"github.com/shopspring/decimal"
)
var TimeMethods = map[string]*object.LibraryFunction{}
var TimeProperties = map[string]*object.LibraryProperty{}
func init() {
RegisterMethod(TimeMethods, "sleep", timeSleep)
RegisterMethod(TimeMethods, "now", timeNow)
RegisterProperty(TimeProperties, "nanosecond", timeNanosecond)
RegisterProperty(TimeProperties, "microsecond", timeMicrosecond)
RegisterProperty(TimeProperties, "millisecond", timeMillisecond)
RegisterProperty(TimeProperties, "second", timeSecond)
RegisterProperty(TimeProperties, "minute", timeMinute)
RegisterProperty(TimeProperties, "hour", timeHour)
RegisterProperty(TimeProperties, "day", timeDay)
RegisterProperty(TimeProperties, "week", timeWeek)
RegisterProperty(TimeProperties, "month", timeMonth)
RegisterProperty(TimeProperties, "year", timeYear)
}
func timeSleep(scope *object.Scope, tok token.Token, args ...object.Object) object.Object {
if len(args) != 1 {
// TODO: error
return nil
}
if args[0].Type() != object.NUMBER {
// TODO: error
return nil
}
ms := args[0].(*object.Number)
time.Sleep(time.Duration(ms.Value.IntPart()) * time.Millisecond)
return nil
}
func timeNow(scope *object.Scope, tok token.Token, args ...object.Object) object.Object {
if len(args) != 0 {
// TODO: error
return nil
}
unix := decimal.NewFromInt(time.Now().Unix())
return &object.Number{Value: unix}
}
// properties
func timeNanosecond(scope *object.Scope, tok token.Token) object.Object {
nanosecond := decimal.NewFromFloat(0.00001)
return &object.Number{Value: nanosecond}
}
func timeMicrosecond(scope *object.Scope, tok token.Token) object.Object {
microsecond := decimal.NewFromFloat(0.0001)
return &object.Number{Value: microsecond}
}
func timeMillisecond(scope *object.Scope, tok token.Token) object.Object {
millisecond := decimal.NewFromFloat(0.001)
return &object.Number{Value: millisecond}
}
func timeSecond(scope *object.Scope, tok token.Token) object.Object {
second := decimal.NewFromInt(1)
return &object.Number{Value: second}
}
func timeMinute(scope *object.Scope, tok token.Token) object.Object {
minute := decimal.NewFromInt(60)
return &object.Number{Value: minute}
}
func timeHour(scope *object.Scope, tok token.Token) object.Object {
hour := decimal.NewFromInt(3600)
return &object.Number{Value: hour}
}
func timeDay(scope *object.Scope, tok token.Token) object.Object {
day := decimal.NewFromInt(86400)
return &object.Number{Value: day}
}
func timeWeek(scope *object.Scope, tok token.Token) object.Object {
week := decimal.NewFromInt(604800)
return &object.Number{Value: week}
}
func timeMonth(scope *object.Scope, tok token.Token) object.Object {
month := decimal.NewFromInt(2592000)
return &object.Number{Value: month}
}
func timeYear(scope *object.Scope, tok token.Token) object.Object {
year := decimal.NewFromInt(31536000)
return &object.Number{Value: year}
}