Skip to content
This repository was archived by the owner on Dec 14, 2020. It is now read-only.

Commit 7c41f1a

Browse files
alanconwayvcabbage
authored andcommitted
Fix Incorrect time calculation for dates before the Unix epoch
AMQP timestamp is a signed value, but decoded from buffer as unsigned. The previous code took the remainder of the unsigned value before converting to signed.
1 parent 5f7db87 commit 7c41f1a

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

decode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,8 @@ func readTimestamp(r *buffer) (time.Time, error) {
946946
}
947947

948948
n, err := r.readUint64()
949-
rem := n % 1000
950-
return time.Unix(int64(n)/1000, int64(rem)*1000000).UTC(), err
949+
ms := int64(n)
950+
return time.Unix(ms/1000, (ms%1000)*1000000).UTC(), err
951951
}
952952

953953
func readInt(r *buffer) (int, error) {

marshal_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ func TestMarshalUnmarshal(t *testing.T) {
212212
}
213213
}
214214

215+
// Regression test for time calculation bug.
216+
// https://github.com/vcabbage/amqp/issues/173
217+
func TestIssue173(t *testing.T) {
218+
var buf buffer
219+
// NOTE: Dates after the Unix Epoch don't trigger the bug, only
220+
// dates that negative Unix time show the problem.
221+
want := time.Date(1969, 03, 21, 0, 0, 0, 0, time.UTC)
222+
err := marshal(&buf, want)
223+
if err != nil {
224+
t.Fatal(err)
225+
}
226+
var got time.Time
227+
err = unmarshal(&buf, &got)
228+
if d := testDiff(want, got); d != "" {
229+
t.Fatal(d)
230+
}
231+
}
232+
215233
func TestReadAny(t *testing.T) {
216234
for _, type_ := range generalTypes {
217235
t.Run(fmt.Sprintf("%T", type_), func(t *testing.T) {

0 commit comments

Comments
 (0)