Skip to content

Commit

Permalink
Fix date manipulation operations
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 30, 2023
1 parent 92c9068 commit f32da1e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
32 changes: 32 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,38 @@ func TestExpr(t *testing.T) {
`TimePlusDay - Duration`,
date,
},
{
`duration("1h") == duration("1h")`,
true,
},
{
`TimePlusDay - Time >= duration("24h")`,
true,
},
{
`duration("1h") > duration("1m")`,
true,
},
{
`duration("1h") < duration("1m")`,
false,
},
{
`duration("1h") >= duration("1m")`,
true,
},
{
`duration("1h") <= duration("1m")`,
false,
},
{
`duration("1h") > duration("1m")`,
true,
},
{
`duration("1h") + duration("1m")`,
time.Hour + time.Minute,
},
{
`1 /* one */ + 2 // two`,
3,
Expand Down
52 changes: 42 additions & 10 deletions vm/runtime/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions vm/runtime/helpers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func Equal(a, b interface{}) bool {
case time.Time:
return x.Equal(y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x == y
}
}
if IsNil(a) && IsNil(b) {
return true
Expand All @@ -120,6 +125,11 @@ func Less(a, b interface{}) bool {
case time.Time:
return x.Before(y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x < y
}
}
panic(fmt.Sprintf("invalid operation: %T < %T", a, b))
}
Expand All @@ -137,6 +147,11 @@ func More(a, b interface{}) bool {
case time.Time:
return x.After(y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x > y
}
}
panic(fmt.Sprintf("invalid operation: %T > %T", a, b))
}
Expand All @@ -154,6 +169,11 @@ func LessOrEqual(a, b interface{}) bool {
case time.Time:
return x.Before(y) || x.Equal(y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x <= y
}
}
panic(fmt.Sprintf("invalid operation: %T <= %T", a, b))
}
Expand All @@ -171,6 +191,11 @@ func MoreOrEqual(a, b interface{}) bool {
case time.Time:
return x.After(y) || x.Equal(y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x >= y
}
}
panic(fmt.Sprintf("invalid operation: %T >= %T", a, b))
}
Expand All @@ -192,6 +217,8 @@ func Add(a, b interface{}) interface{} {
switch y := b.(type) {
case time.Time:
return y.Add(x)
case time.Duration:
return x + y
}
}
panic(fmt.Sprintf("invalid operation: %T + %T", a, b))
Expand All @@ -204,6 +231,13 @@ func Subtract(a, b interface{}) interface{} {
switch y := b.(type) {
case time.Time:
return x.Sub(y)
case time.Duration:
return x.Add(-y)
}
case time.Duration:
switch y := b.(type) {
case time.Duration:
return x - y
}
}
panic(fmt.Sprintf("invalid operation: %T - %T", a, b))
Expand Down

0 comments on commit f32da1e

Please sign in to comment.