[GODRIVER-434] Fix encoding of *time.Time Created: 29/May/18  Updated: 28/Oct/23  Resolved: 29/May/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.0.5
Fix Version/s: 0.0.6

Type: Bug Priority: Major - P3
Reporter: Kristofer Brandow (Inactive) Assignee: Kristofer Brandow (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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.|



 Comments   
Comment by Githook User [ 29/May/18 ]

Author:

{'username': 'skriptble', 'name': 'Kris Brandow', 'email': 'kris@mongodb.com'}

Message: Merge branch 'pr/62'

GODRIVER-434

Change-Id: I9bc957771c7c411dcafe15a1b8b99adc0222e024
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/07a5deb00a9f938a22f127de222d28e1273bea80

Comment by Kristofer Brandow (Inactive) [ 29/May/18 ]

Pull Request: https://github.com/mongodb/mongo-go-driver/pull/62.

Generated at Thu Feb 08 08:34:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.