From 199f791885764c1736178a1132f08706efef8410 Mon Sep 17 00:00:00 2001 From: Tristan Swadell Date: Thu, 21 May 2020 17:04:56 -0700 Subject: [PATCH] Add runtime support for timestamp from Unix epoch (#358) --- common/types/int.go | 8 ++++++++ common/types/int_test.go | 7 +++++++ interpreter/interpreter_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/common/types/int.go b/common/types/int.go index c36accc1..8c586f59 100644 --- a/common/types/int.go +++ b/common/types/int.go @@ -18,6 +18,7 @@ import ( "fmt" "reflect" "strconv" + "time" "github.com/golang/protobuf/ptypes" @@ -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 } diff --git a/common/types/int_test.go b/common/types/int_test.go index 8c569163..95ccc347 100644 --- a/common/types/int_test.go +++ b/common/types/int_test.go @@ -18,6 +18,7 @@ import ( "math" "reflect" "testing" + "time" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/ptypes" @@ -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) { diff --git a/interpreter/interpreter_test.go b/interpreter/interpreter_test.go index 756b113a..8438ec0f 100644 --- a/interpreter/interpreter_test.go +++ b/interpreter/interpreter_test.go @@ -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')`,