Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-434

Fix encoding of *time.Time

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 0.0.6
    • 0.0.5
    • BSON
    • None

    Description

      Came across a problem with encoding *time.Time. It turned out that it is ignored during encoding process.
      Code to reproduce:

      package main
       
      import (
      	"log"
      	"time"
       
      	"github.com/mongodb/mongo-go-driver/bson"
      )
       
      type testStruct struct {
      	FieldA time.Time  `bson:"field_a"`
      	FieldB *time.Time `bson:"field_b"`
      }
       
      func main() {
      	now := time.Now()
      	test := testStruct{
      		FieldA: now,
      		FieldB: &now,
      	}
      	data, err := bson.Marshal(test)
      	if err != nil {
      		log.Fatalf("Failed to marshal. Error: %v", err)
      	}
      	unTest := testStruct{}
      	err = bson.Unmarshal(data, &unTest)
      	if err != nil {
      		log.Fatalf("Failed to marshal. Error: %v", err)
      	}
      	log.Printf("Marshaled: %+v", test)
      	log.Printf("Unmarshaled:%+v", unTest)
      }
      

      Will give something like this:

       2018/05/25 11:24:09 Marshaled: {FieldA:2018-05-25 11:24:09.703404724 +1000 AEST m=+0.001358642 FieldB:2018-05-25 11:24:09.703404724 +1000 AEST m=+0.001358642}
      2018/05/25 11:24:09 Unmarshaled:{FieldA:2018-05-25 11:24:09.703 +1000 AEST FieldB:0001-01-01 00:00:00 +0000 UTC}
      

      So, value for FiledB was not encoded.|

      Attachments

        Activity

          People

            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: