Skip to content

Commit db81f4c

Browse files
authored
Merge pull request globalsign#13 from globalsign/bugfix/reenjii-fix-json-timezone
Fix timezone handling
2 parents feb20d2 + 9c15c93 commit db81f4c

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
1616
* Improved connection handling ([details](https://github.com/globalsign/mgo/pull/5))
1717
* Hides SASL warnings ([details](https://github.com/globalsign/mgo/pull/7))
1818
* Improved multi-document transaction performance ([details](https://github.com/globalsign/mgo/pull/10), [more](https://github.com/globalsign/mgo/pull/11))
19+
* Fixes timezone handling ([details](https://github.com/go-mgo/mgo/pull/464))
1920
* Fixes cursor timeouts ([detials](https://jira.mongodb.org/browse/SERVER-24899))
2021

2122
---
@@ -27,5 +28,6 @@ Further PR's (with tests) are welcome, but please maintain backwards compatibili
2728
* @eaglerayp
2829
* @drichelson
2930
* @jameinel
31+
* @Reenjii
3032
* @smoya
3133
* @wgallagher

bson/json.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"fmt"
77
"strconv"
8+
"strings"
89
"time"
910

1011
"github.com/globalsign/mgo/internal/json"
@@ -156,7 +157,7 @@ func jencBinaryType(v interface{}) ([]byte, error) {
156157
return fbytes(`{"$binary":"%s","$type":"0x%x"}`, out, in.Kind), nil
157158
}
158159

159-
const jdateFormat = "2006-01-02T15:04:05.999Z"
160+
const jdateFormat = "2006-01-02T15:04:05.999Z07:00"
160161

161162
func jdecDate(data []byte) (interface{}, error) {
162163
var v struct {
@@ -170,13 +171,15 @@ func jdecDate(data []byte) (interface{}, error) {
170171
v.S = v.Func.S
171172
}
172173
if v.S != "" {
174+
var errs []string
173175
for _, format := range []string{jdateFormat, "2006-01-02"} {
174176
t, err := time.Parse(format, v.S)
175177
if err == nil {
176178
return t, nil
177179
}
180+
errs = append(errs, err.Error())
178181
}
179-
return nil, fmt.Errorf("cannot parse date: %q", v.S)
182+
return nil, fmt.Errorf("cannot parse date: %q [%s]", v.S, strings.Join(errs, ", "))
180183
}
181184

182185
var vn struct {

bson/json_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ var jsonTests = []jsonTest{
3434
{
3535
a: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.UTC),
3636
b: `{"$date":"2016-05-15T01:02:03.004Z"}`,
37+
}, {
38+
a: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.FixedZone("CET", 60*60)),
39+
b: `{"$date":"2016-05-15T01:02:03.004+01:00"}`,
3740
}, {
3841
b: `{"$date": {"$numberLong": "1002"}}`,
3942
c: time.Date(1970, 1, 1, 0, 0, 1, 2e6, time.UTC),
4043
}, {
4144
b: `ISODate("2016-05-15T01:02:03.004Z")`,
4245
c: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.UTC),
46+
}, {
47+
b: `ISODate("2016-05-15T01:02:03.004-07:00")`,
48+
c: time.Date(2016, 5, 15, 1, 2, 3, 4000000, time.FixedZone("PDT", -7*60*60)),
4349
}, {
4450
b: `new Date(1000)`,
4551
c: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC),
@@ -180,6 +186,11 @@ func (s *S) TestJSON(c *C) {
180186
value = zerov.Elem().Interface()
181187
}
182188
c.Logf("Loaded: %#v", value)
189+
if ctime, ok := item.c.(time.Time); ok {
190+
// time.Time must be compared with time.Time.Equal and not reflect.DeepEquals
191+
c.Assert(ctime.Equal(value.(time.Time)), Equals, true)
192+
continue
193+
}
183194
c.Assert(value, DeepEquals, item.c)
184195
}
185196
}

0 commit comments

Comments
 (0)