[GODRIVER-300] Support constructing *Value from time.Time instances Created: 24/Mar/18  Updated: 28/Oct/23  Resolved: 24/Oct/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 0.0.17

Type: Improvement Priority: Major - P3
Reporter: Sam Kleinman (Inactive) Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: beta, evg, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
is caused by GODRIVER-363 BSON encode and decoder do not suppor... Closed

 Description   

The ElementEncoder supports encoding a time.Time value into a *bson.Element, add this same support to the ValueConstructor.



 Comments   
Comment by Githook User [ 24/Oct/18 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@10gen.com'}

Message: GODRIVER-300 add Time(time.Time) to ValueConstructor

Change-Id: I986962c2c67510b4c68e87bf52712ee41a90dd58
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/e763e207f402c58b8e0355d5335ac3b0e597ec15

Comment by Sam Kleinman (Inactive) [ 18/May/18 ]

The element and value constructors for DateTime (time?) objects should take time.Time objects rather than in64 objects.

Comment by Kristofer Brandow (Inactive) [ 12/Apr/18 ]

thomas.delacour I'll open a bug for this.

Comment by Thomas Delacour (Inactive) [ 10/Apr/18 ]

kris.brandow Is the current driver supposed to support marshaling / unmarshaling time.Time objects embedded in structs? I am currently encountering a silent bug with this.

Comment by Ramón Berrutti [ 29/Mar/18 ]

I was thinking the same yesterday...
I am using this know, but a native function will be nice, you already have a function that return a Time object.

bson.EC.DateTime("date", dateTime.UnixNano()/int64(time.Millisecond)),

Comment by Karan Razdan [ 28/Mar/18 ]

I think what Sam is trying to say is bson can create 'DateTime' objects which are of type int64. We should be able to pass in the time.Time objects instead of int64 which restricts us to Unix time.

// DateTime creates a datetime element with the given key and value.
func (ElementConstructor) DateTime(key string, dt int64) *Element {
	size := uint32(1 + len(key) + 1 + 8)
	elem := newElement(0, 1+uint32(len(key))+1)
	elem.value.data = make([]byte, size)
 
	_, err := elements.DateTime.Element(0, elem.value.data, key, dt)
	if err != nil {
		panic(err)
	}
 
	return elem
}

Comment by Kristofer Brandow (Inactive) [ 26/Mar/18 ]

Hi Sam,

Can you clarify what you mean here? e.g. are you looking to have support for unmarshaling into a time.Time object embedded in a struct?

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