-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime.go
209 lines (182 loc) · 5.21 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
// Package time provides time.Time wrappers for the skylark embedded language.
package time
import (
"errors"
"time"
"github.com/google/skylark"
"github.com/google/skylark/syntax"
)
// Now returns the current Time.
func Now(_ *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
if len(args) != 0 {
return nil, errors.New("too many args")
}
if len(kwargs) != 0 {
return nil, errors.New("too many kwargs")
}
return Time(time.Now()), nil
}
// Delta returns a duration created from kwargs. Expected "hours", "minutes",
// "seconds", "milliseconds", or "nanoseconds", and assigned an int.
func Delta(_ *skylark.Thread, _ *skylark.Builtin, args skylark.Tuple, kwargs []skylark.Tuple) (skylark.Value, error) {
if len(args) != 0 {
return nil, errors.New("too many args")
}
var d time.Duration
for _, t := range kwargs {
if len(t) != 2 {
panic("invalid kwarg")
}
s, ok := t[0].(skylark.String)
if !ok {
panic("invalid kwarg name")
}
i, ok := t[1].(skylark.Int)
if !ok {
return nil, errors.New("invalid value for timedelta arg, must be int")
}
v, ok := i.Int64()
if !ok {
return nil, errors.New("numeric value overflows int64")
}
switch s {
case "hours":
d += time.Hour * time.Duration(v)
case "minutes":
d += time.Minute * time.Duration(v)
case "seconds":
d += time.Second * time.Duration(v)
case "milliseconds":
d += time.Millisecond * time.Duration(v)
case "nanoseconds":
d += time.Nanosecond * time.Duration(v)
default:
return nil, errors.New("invalid duration unit: " + string(s))
}
}
return Duration(d), nil
}
// Time is the type of a Skylark time.Time.
type Time time.Time
// Zero is the zero time.
var Zero = Time(time.Time{})
// String returns the string representation of this Time.
func (t Time) String() string {
return time.Time(t).String()
}
// Type returns the typename of the value.
func (t Time) Type() string { return "time" }
// Freeze makes the value immutable.
func (t Time) Freeze() {} // immutable
// Truth reports the python-truthiness of the value.
func (t Time) Truth() skylark.Bool { return skylark.Bool(!time.Time(t).IsZero()) }
// Hash returns a hash of the value for used in sorting.
func (t Time) Hash() (uint32, error) { return hashString(t.String()), nil }
// CompareSameType compares this Time to another.
func (t Time) CompareSameType(op syntax.Token, y skylark.Value, depth int) (bool, error) {
t1 := time.Time(t)
t2 := time.Time(y.(Time))
switch op {
case syntax.EQL:
return t1.Equal(t2), nil
case syntax.NEQ:
return !t1.Equal(t2), nil
case syntax.LE:
return t1.Equal(t2) || t1.Before(t2), nil
case syntax.LT:
return t1.Before(t2), nil
case syntax.GE:
return t1.Equal(t2) || t1.After(t2), nil
case syntax.GT:
return t1.After(t2), nil
}
panic(op)
}
// Binary implements binary operators.
func (t Time) Binary(op syntax.Token, y skylark.Value, side skylark.Side) (skylark.Value, error) {
if side == skylark.Right {
return nil, nil
}
if time2, ok := y.(Time); ok {
return t.binaryTime(op, time2)
}
if d, ok := y.(Duration); ok {
return t.binaryDuration(op, d)
}
return nil, nil
}
func (t Time) binaryTime(op syntax.Token, y Time) (skylark.Value, error) {
switch op {
case syntax.MINUS:
return Duration(time.Time(t).Sub(time.Time(y))), nil
}
return nil, nil
}
func (t Time) binaryDuration(op syntax.Token, y Duration) (skylark.Value, error) {
switch op {
case syntax.MINUS:
return Time(time.Time(t).Add(-time.Duration(y))), nil
case syntax.PLUS:
return Time(time.Time(t).Add(time.Duration(y))), nil
}
return nil, nil
}
// hashString computes the FNV hash of s. Copied from github.com/google/skylark/hashtable.go
func hashString(s string) uint32 {
var h uint32
for i := 0; i < len(s); i++ {
h ^= uint32(s[i])
h *= 16777619
}
return h
}
// Duration represents a span of time.
type Duration time.Duration
// String returns the string representation of this Time.
func (d Duration) String() string {
return time.Duration(d).String()
}
// Type returns the typename of the value.
func (d Duration) Type() string { return "time" }
// Freeze makes the value immutable.
func (d Duration) Freeze() {} // immutable
// Truth reports the python-truthiness of the value.
func (d Duration) Truth() skylark.Bool { return skylark.Bool(d != 0) }
// Hash returns a hash of the value for used in sorting.
func (d Duration) Hash() (uint32, error) { return hashString(d.String()), nil }
// CompareSameType compares this Time to another.
func (d Duration) CompareSameType(op syntax.Token, y skylark.Value, depth int) (bool, error) {
d2 := y.(Duration)
switch op {
case syntax.EQL:
return d == d2, nil
case syntax.NEQ:
return d != d2, nil
case syntax.LE:
return d <= d2, nil
case syntax.LT:
return d < d2, nil
case syntax.GE:
return d >= d2, nil
case syntax.GT:
return d > d2, nil
}
panic(op)
}
// Binary implements binary operators.
func (d Duration) Binary(op syntax.Token, y skylark.Value, side skylark.Side) (skylark.Value, error) {
if side == skylark.Right {
return nil, nil
}
d2, ok := y.(Duration)
if !ok {
return nil, nil
}
switch op {
case syntax.MINUS:
return Duration(d - d2), nil
case syntax.PLUS:
return Duration(d + d2), nil
}
return nil, nil
}