Skip to content

Commit

Permalink
Add runtime support for timestamp from Unix epoch (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones authored May 22, 2020
1 parent aa4b2a7 commit 199f791
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions common/types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"reflect"
"strconv"
"time"

"github.com/golang/protobuf/ptypes"

Expand Down Expand Up @@ -163,6 +164,13 @@ func (i Int) ConvertToType(typeVal ref.Type) ref.Val {
return Double(i)
case StringType:
return String(fmt.Sprintf("%d", int64(i)))
case TimestampType:
t := time.Unix(int64(i), 0)
ts, err := ptypes.TimestampProto(t)
if err != nil {
return NewErr(err.Error())
}
return Timestamp{Timestamp: ts}
case TypeType:
return IntType
}
Expand Down
7 changes: 7 additions & 0 deletions common/types/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"reflect"
"testing"
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
Expand Down Expand Up @@ -178,6 +179,12 @@ func TestInt_ConvertToType(t *testing.T) {
if !IsError(Int(-4).ConvertToType(DurationType)) {
t.Error("Got duration, expected error.")
}
tm := time.Unix(946684800, 0)
ts, _ := ptypes.TimestampProto(tm)
celts := Timestamp{Timestamp: ts}
if !Int(946684800).ConvertToType(TimestampType).Equal(celts).(Bool) {
t.Error("unsuccessful type conversion to timestamp")
}
}

func TestInt_Divide(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,30 @@ var (
RepeatedInt32: []int32{0, 2},
},
},
{
name: "timestamp_eq_timestamp",
expr: `timestamp(0) == timestamp(0)`,
},
{
name: "timestamp_eq_timestamp",
expr: `timestamp(1) != timestamp(2)`,
},
{
name: "timestamp_lt_timestamp",
expr: `timestamp(0) < timestamp(1)`,
},
{
name: "timestamp_le_timestamp",
expr: `timestamp(2) <= timestamp(2)`,
},
{
name: "timestamp_gt_timestamp",
expr: `timestamp(1) > timestamp(0)`,
},
{
name: "timestamp_ge_timestamp",
expr: `timestamp(2) >= timestamp(2)`,
},
{
name: "string_to_timestamp",
expr: `timestamp('1986-04-26T01:23:40Z')`,
Expand Down

0 comments on commit 199f791

Please sign in to comment.