@@ -9,6 +9,9 @@ package objectid
99import (
1010 "testing"
1111
12+ "encoding/binary"
13+ "time"
14+
1215 "github.com/stretchr/testify/require"
1316)
1417
@@ -39,3 +42,42 @@ func TestFromHex_WrongLength(t *testing.T) {
3942 _ , err := FromHex ("deadbeef" )
4043 require .Equal (t , ErrInvalidHex , err )
4144}
45+
46+ func TestTimeStamp (t * testing.T ) {
47+ testCases := []struct {
48+ Hex string
49+ Expected string
50+ }{
51+ {
52+ "000000001111111111111111" ,
53+ "1970-01-01 00:00:00 +0000 UTC" ,
54+ },
55+ {
56+ "7FFFFFFF1111111111111111" ,
57+ "2038-01-19 03:14:07 +0000 UTC" ,
58+ },
59+ {
60+ "800000001111111111111111" ,
61+ "2038-01-19 03:14:08 +0000 UTC" ,
62+ },
63+ {
64+ "FFFFFFFF1111111111111111" ,
65+ "2106-02-07 06:28:15 +0000 UTC" ,
66+ },
67+ }
68+
69+ for _ , testcase := range testCases {
70+ id , err := FromHex (testcase .Hex )
71+ require .NoError (t , err )
72+ secs := int64 (binary .BigEndian .Uint32 (id [0 :4 ]))
73+ timestamp := time .Unix (secs , 0 ).UTC ()
74+ require .Equal (t , testcase .Expected , timestamp .String ())
75+ }
76+
77+ }
78+
79+ func TestCounterOverflow (t * testing.T ) {
80+ objectIDCounter = 0xFFFFFFFF
81+ New ()
82+ require .Equal (t , uint32 (0 ), objectIDCounter )
83+ }
0 commit comments