Skip to content

Commit efac15b

Browse files
authored
chip-ingress: newevent - refactor timestamp to millisecond and add 0 check (#1576)
* refactor timestamp to millisecond and add 0 check * undo
1 parent 98903c7 commit efac15b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

pkg/chipingress/client.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,14 @@ func NewEvent(domain, entity string, payload []byte, attributes map[string]any)
194194
attributes = make(map[string]any)
195195
}
196196

197-
if val, ok := attributes["recordedtime"].(time.Time); ok {
198-
event.SetExtension("recordedtime", val.UTC())
199-
} else {
200-
event.SetExtension("recordedtime", ce.Timestamp{Time: time.Now().UTC()})
197+
recordedTime := time.Now()
198+
if val, ok := attributes["recordedtime"].(time.Time); ok && !val.IsZero() {
199+
recordedTime = val
201200
}
201+
recordedTime = recordedTime.UTC().Truncate(time.Millisecond)
202+
event.SetExtension("recordedtime", ce.Timestamp{Time: recordedTime})
202203

203-
if val, ok := attributes["time"].(time.Time); ok {
204+
if val, ok := attributes["time"].(time.Time); ok && !val.IsZero() {
204205
event.SetTime(val.UTC())
205206
}
206207
if val, ok := attributes["datacontenttype"].(string); ok {

pkg/chipingress/client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestNewEvent(t *testing.T) {
6767
"datacontenttype": "application/protobuf",
6868
"dataschema": "https://example.com/schema",
6969
"subject": "example-subject",
70-
"time": time.Now(),
70+
"time": time.Now().Add(-5 * time.Second),
7171
}
7272
assert.NoError(t, err)
7373

@@ -87,7 +87,6 @@ func TestNewEvent(t *testing.T) {
8787
assert.Equal(t, "example-subject", event.Subject())
8888
assert.Equal(t, attributes["time"].(time.Time).UTC(), event.Time())
8989
assert.NotEmpty(t, event.Extensions()["recordedtime"])
90-
assert.NotEmpty(t, event.Extensions()["recordedtime"])
9190
assert.True(t, event.Extensions()["recordedtime"].(ce.Timestamp).Time.After(attributes["time"].(time.Time)))
9291

9392
// Assert the event data was set as expected

0 commit comments

Comments
 (0)