Skip to content

Commit

Permalink
Fixed conversion of unix timestamp to golang time (apache#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
massakam authored and merlimat committed Feb 22, 2019
1 parent 5127351 commit 40b374d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pulsar-client-go/pulsar/c_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,9 @@ func latestMessageID() *messageID {
}

func timeFromUnixTimestampMillis(timestamp C.ulonglong) time.Time {
ts := int64(timestamp)
seconds := ts / int64(time.Millisecond)
millis := ts - seconds
nanos := millis * int64(time.Millisecond)
ts := int64(timestamp) * int64(time.Millisecond)
seconds := ts / int64(time.Second)
nanos := ts - (seconds * int64(time.Second))
return time.Unix(seconds, nanos)
}

Expand Down
4 changes: 4 additions & 0 deletions pulsar-client-go/pulsar/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,22 @@ func TestConsumer(t *testing.T) {
ctx := context.Background()

for i := 0; i < 10; i++ {
sendTime := time.Now()
if err := producer.Send(ctx, ProducerMessage{
Payload: []byte(fmt.Sprintf("hello-%d", i)),
}); err != nil {
t.Fatal(err)
}

msg, err := consumer.Receive(ctx)
recvTime := time.Now()
assert.Nil(t, err)
assert.NotNil(t, msg)

assert.Equal(t, string(msg.Payload()), fmt.Sprintf("hello-%d", i))
assert.Equal(t, string(msg.Topic()), "persistent://public/default/my-topic")
assert.True(t, sendTime.Unix() <= msg.PublishTime().Unix())
assert.True(t, recvTime.Unix() >= msg.PublishTime().Unix())

consumer.Ack(msg)
}
Expand Down

0 comments on commit 40b374d

Please sign in to comment.