diff --git a/cursor_test.go b/cursor_test.go index e438c715..e854d3a2 100644 --- a/cursor_test.go +++ b/cursor_test.go @@ -145,10 +145,12 @@ func (s *RethinkSuite) TestCursorStruct(c *test.C) { } func (s *RethinkSuite) TestCursorStructPseudoTypes(c *test.C) { + var zeroTime time.Time t := time.Now() res, err := Expr(map[string]interface{}{ "T": time.Unix(t.Unix(), 0).In(time.UTC), + "Z": zeroTime, "B": []byte("hello"), }).Run(sess) c.Assert(err, test.IsNil) @@ -159,6 +161,7 @@ func (s *RethinkSuite) TestCursorStructPseudoTypes(c *test.C) { c.Assert(res.Type(), test.Equals, "Cursor") c.Assert(response.T.Equal(time.Unix(t.Unix(), 0)), test.Equals, true) + c.Assert(response.Z.Equal(zeroTime), test.Equals, true) c.Assert(response.B, jsonEquals, []byte("hello")) } diff --git a/encoding/encoder_types.go b/encoding/encoder_types.go index 9f893ed3..83cefe8e 100644 --- a/encoding/encoder_types.go +++ b/encoding/encoder_types.go @@ -2,6 +2,7 @@ package encoding import ( "encoding/base64" + "math" "reflect" "time" ) @@ -262,9 +263,17 @@ func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc { func timePseudoTypeEncoder(v reflect.Value) interface{} { t := v.Interface().(time.Time) + timeVal := float64(t.UnixNano()) / float64(time.Second) + + // use seconds-since-epoch precision if time.Time `t` + // is before the oldest nanosecond time + if t.Before(time.Unix(0, math.MinInt64)) { + timeVal = float64(t.Unix()) + } + return map[string]interface{}{ "$reql_type$": "TIME", - "epoch_time": float64(t.UnixNano()) / 1000 / 1000 / 1000, //milliseconds + "epoch_time": timeVal, "timezone": "+00:00", } } diff --git a/gorethink_test.go b/gorethink_test.go index 1f128ded..a63ad4f4 100644 --- a/gorethink_test.go +++ b/gorethink_test.go @@ -218,6 +218,7 @@ type Y struct { type PseudoTypes struct { T time.Time + Z time.Time B []byte }