@@ -32,6 +32,8 @@ import (
3232 "encoding/json"
3333 "encoding/xml"
3434 "errors"
35+ "fmt"
36+ "math/rand"
3537 "net/url"
3638 "reflect"
3739 "strings"
@@ -286,12 +288,12 @@ func (s *S) TestPtrInline(c *C) {
286288 },
287289 {
288290 // nil embed struct
289- In : & inlinePtrStruct {A : 3 },
291+ In : & inlinePtrStruct {A : 3 },
290292 Out : bson.M {"a" : 3 },
291293 },
292294 {
293295 // nil embed struct
294- In : & inlinePtrPtrStruct {B : 5 },
296+ In : & inlinePtrPtrStruct {B : 5 },
295297 Out : bson.M {"b" : 5 },
296298 },
297299 }
@@ -1205,18 +1207,18 @@ type inlineBadKeyMap struct {
12051207 M map [int ]int `bson:",inline"`
12061208}
12071209type inlineUnexported struct {
1208- M map [string ]interface {} `bson:",inline"`
1209- unexported `bson:",inline"`
1210+ M map [string ]interface {} `bson:",inline"`
1211+ unexported `bson:",inline"`
12101212}
12111213type MStruct struct {
12121214 M int `bson:"m,omitempty"`
12131215}
12141216type inlinePtrStruct struct {
1215- A int
1217+ A int
12161218 * MStruct `bson:",inline"`
12171219}
12181220type inlinePtrPtrStruct struct {
1219- B int
1221+ B int
12201222 * inlinePtrStruct `bson:",inline"`
12211223}
12221224type unexported struct {
@@ -1274,11 +1276,11 @@ func (s ifaceSlice) GetBSON() (interface{}, error) {
12741276
12751277type (
12761278 MyString string
1277- MyBytes []byte
1278- MyBool bool
1279- MyD []bson.DocElem
1280- MyRawD []bson.RawDocElem
1281- MyM map [string ]interface {}
1279+ MyBytes []byte
1280+ MyBool bool
1281+ MyD []bson.DocElem
1282+ MyRawD []bson.RawDocElem
1283+ MyM map [string ]interface {}
12821284)
12831285
12841286var (
@@ -1989,3 +1991,49 @@ func (s *S) TestMarshalRespectNil(c *C) {
19891991 c .Assert (testStruct2 .Map , NotNil )
19901992 c .Assert (testStruct2 .MapPtr , NotNil )
19911993}
1994+
1995+ func (s * S ) TestMongoTimestampTime (c * C ) {
1996+ t := time .Now ()
1997+ ts , err := bson .NewMongoTimestamp (t , 123 )
1998+ c .Assert (err , IsNil )
1999+ c .Assert (ts .Time ().Unix (), Equals , t .Unix ())
2000+ }
2001+
2002+ func (s * S ) TestMongoTimestampCounter (c * C ) {
2003+ rnd := rand .Uint32 ()
2004+ ts , err := bson .NewMongoTimestamp (time .Now (), rnd )
2005+ c .Assert (err , IsNil )
2006+ c .Assert (ts .Counter (), Equals , rnd )
2007+ }
2008+
2009+ func (s * S ) TestMongoTimestampError (c * C ) {
2010+ t := time .Date (1969 , time .December , 31 , 23 , 59 , 59 , 999 , time .UTC )
2011+ ts , err := bson .NewMongoTimestamp (t , 321 )
2012+ c .Assert (int64 (ts ), Equals , int64 (- 1 ))
2013+ c .Assert (err , ErrorMatches , "invalid value for time" )
2014+ }
2015+
2016+ func ExampleNewMongoTimestamp () {
2017+
2018+ var counter uint32 = 1
2019+ var t time.Time
2020+
2021+ for i := 1 ; i <= 3 ; i ++ {
2022+
2023+ if c := time .Now (); t .Unix () == c .Unix () {
2024+ counter ++
2025+ } else {
2026+ t = c
2027+ counter = 1
2028+ }
2029+
2030+ ts , err := bson .NewMongoTimestamp (t , counter )
2031+ if err != nil {
2032+ fmt .Printf ("NewMongoTimestamp error: %v" , err )
2033+ } else {
2034+ fmt .Printf ("NewMongoTimestamp encoded timestamp: %d\n " , ts )
2035+ }
2036+
2037+ time .Sleep (500 * time .Millisecond )
2038+ }
2039+ }
0 commit comments