Skip to content

Commit f32da1e

Browse files
committed
Fix date manipulation operations
1 parent 92c9068 commit f32da1e

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

expr_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,38 @@ func TestExpr(t *testing.T) {
944944
`TimePlusDay - Duration`,
945945
date,
946946
},
947+
{
948+
`duration("1h") == duration("1h")`,
949+
true,
950+
},
951+
{
952+
`TimePlusDay - Time >= duration("24h")`,
953+
true,
954+
},
955+
{
956+
`duration("1h") > duration("1m")`,
957+
true,
958+
},
959+
{
960+
`duration("1h") < duration("1m")`,
961+
false,
962+
},
963+
{
964+
`duration("1h") >= duration("1m")`,
965+
true,
966+
},
967+
{
968+
`duration("1h") <= duration("1m")`,
969+
false,
970+
},
971+
{
972+
`duration("1h") > duration("1m")`,
973+
true,
974+
},
975+
{
976+
`duration("1h") + duration("1m")`,
977+
time.Hour + time.Minute,
978+
},
947979
{
948980
`1 /* one */ + 2 // two`,
949981
3,

vm/runtime/generated.go

+42-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/runtime/helpers/main.go

+34
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func Equal(a, b interface{}) bool {
100100
case time.Time:
101101
return x.Equal(y)
102102
}
103+
case time.Duration:
104+
switch y := b.(type) {
105+
case time.Duration:
106+
return x == y
107+
}
103108
}
104109
if IsNil(a) && IsNil(b) {
105110
return true
@@ -120,6 +125,11 @@ func Less(a, b interface{}) bool {
120125
case time.Time:
121126
return x.Before(y)
122127
}
128+
case time.Duration:
129+
switch y := b.(type) {
130+
case time.Duration:
131+
return x < y
132+
}
123133
}
124134
panic(fmt.Sprintf("invalid operation: %T < %T", a, b))
125135
}
@@ -137,6 +147,11 @@ func More(a, b interface{}) bool {
137147
case time.Time:
138148
return x.After(y)
139149
}
150+
case time.Duration:
151+
switch y := b.(type) {
152+
case time.Duration:
153+
return x > y
154+
}
140155
}
141156
panic(fmt.Sprintf("invalid operation: %T > %T", a, b))
142157
}
@@ -154,6 +169,11 @@ func LessOrEqual(a, b interface{}) bool {
154169
case time.Time:
155170
return x.Before(y) || x.Equal(y)
156171
}
172+
case time.Duration:
173+
switch y := b.(type) {
174+
case time.Duration:
175+
return x <= y
176+
}
157177
}
158178
panic(fmt.Sprintf("invalid operation: %T <= %T", a, b))
159179
}
@@ -171,6 +191,11 @@ func MoreOrEqual(a, b interface{}) bool {
171191
case time.Time:
172192
return x.After(y) || x.Equal(y)
173193
}
194+
case time.Duration:
195+
switch y := b.(type) {
196+
case time.Duration:
197+
return x >= y
198+
}
174199
}
175200
panic(fmt.Sprintf("invalid operation: %T >= %T", a, b))
176201
}
@@ -192,6 +217,8 @@ func Add(a, b interface{}) interface{} {
192217
switch y := b.(type) {
193218
case time.Time:
194219
return y.Add(x)
220+
case time.Duration:
221+
return x + y
195222
}
196223
}
197224
panic(fmt.Sprintf("invalid operation: %T + %T", a, b))
@@ -204,6 +231,13 @@ func Subtract(a, b interface{}) interface{} {
204231
switch y := b.(type) {
205232
case time.Time:
206233
return x.Sub(y)
234+
case time.Duration:
235+
return x.Add(-y)
236+
}
237+
case time.Duration:
238+
switch y := b.(type) {
239+
case time.Duration:
240+
return x - y
207241
}
208242
}
209243
panic(fmt.Sprintf("invalid operation: %T - %T", a, b))

0 commit comments

Comments
 (0)