[GODRIVER-1001] Current time zone mode for time.Time handling wrt timezones Created: 26/Apr/19  Updated: 27/Oct/23  Resolved: 24/Jul/20

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

Type: Task Priority: Major - P3
Reporter: Sam Kleinman (Inactive) Assignee: Unassigned
Resolution: Gone away Votes: 0
Labels: evg
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-1355 Create TimeCodec to match mgo behavior Closed

 Description   

the evergreen team observes that timezones are handled differently with the new bson library relative to mgobson. I don't think there's a problem in terms of correctness, but it causes our tests to fail in ways that are irritating, and requires some context in order to fix.

I believe that mgobson converted time.Time back to the current time zone, and the new driver leaves them in UTC. This should be fully documented in any migration material, and potentially in the API documentation, but it might be nice to build/provide a set of codecs for compatibility mode, to reduce migration friction.



 Comments   
Comment by Isabella Siu (Inactive) [ 17/Oct/19 ]

I'm going to remove this from the mgocompat epic, as this isn't a compatibility problem with globalsign's mgo, but I will leave this open, as we are open to adding non-mgocompat options to the codecs in the future.

Comment by Sam Kleinman (Inactive) [ 16/Oct/19 ]

I was using our (10gen) fork rather than the globalsign one, your code keeps it in EDT for me:

package main
 
import (
	"fmt"
	"time"
 
	"gopkg.in/mgo.v2/bson"
)
 
type timeThing struct {
	Foo time.Time
}
 
func main() {
	doc := bson.D{{Name: "foo", Value: time.Now()}}
	fmt.Println(doc[0].Value)
 
	enc, err := bson.Marshal(doc)
	if err != nil {
		fmt.Println(":(")
	}
 
	var doc2 timeThing
	if err = bson.Unmarshal(enc, &doc2); err != nil {
		fmt.Println(":(")
	}
 
	fmt.Println(doc2.Foo)
}

go run cmd/mgobson/mgobson.go 
2019-10-16 17:38:47.104422889 -0400 EDT m=+0.000230197
2019-10-16 17:38:47.104 -0400 EDT

Comment by Isabella Siu (Inactive) [ 16/Oct/19 ]

I'm using the master branch of "github.com/globalsign/mgo/bson" and it changed to UTC. Maybe we're using different mgo versions or calling a different function? Can you show me the test code that reproduces your issue? The code I used is below:

import ( 
 "fmt"
 "time"
 "github.com/globalsign/mgo/bson"
)
 
type timeThing struct {
 Foo time.Time
}
 
func main() {
 doc := bson.D{{"foo", time.Now()}}
 fmt.Println(doc[0].Value)
 enc, err := bson.Marshal(doc) 
 if err != nil { fmt.Println(":(") }
 
 var doc2 timeThing 
 err = bson.Unmarshal(enc, &doc2)
 if err != nil { fmt.Println(":(") }
 fmt.Println(doc2.Foo)
}

Comment by Sam Kleinman (Inactive) [ 16/Oct/19 ]

Yes exactly. The behavior is different in mgo

Comment by Isabella Siu (Inactive) [ 16/Oct/19 ]

Hi sam.kleinman,

Can you give us a repro of this? When I marshaled and unmarshaled a time.Time using mgobson it went from EDT to UTC.

Comment by Bernie Hackett [ 26/Apr/19 ]

As a point of comparison, PyMongo gives you datetimes in UTC by default, but if you configure a timezone (and have pytz installed in the python environment) we'll give you back datetimes in whatever local time you configure.

https://api.mongodb.com/python/current/examples/datetimes.html#saving-datetimes-with-timezones

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