Browse Source

codec: support go 1.4 alternative for time.AppendFormat

json handle calls time.AppendFormat.
This is not available prior to go 1.5.

So we use build tags to build appropriately.

Updates #306
Ugorji Nwoke 6 years ago
parent
commit
f20d06940e
3 changed files with 29 additions and 1 deletions
  1. 12 0
      codec/goversion_fmt_time_gte_go15.go
  2. 15 0
      codec/goversion_fmt_time_lt_go15.go
  3. 2 1
      codec/json.go

+ 12 - 0
codec/goversion_fmt_time_gte_go15.go

@@ -0,0 +1,12 @@
+// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
+// Use of this source code is governed by a MIT license found in the LICENSE file.
+
+// +build go1.5
+
+package codec
+
+import "time"
+
+func fmtTime(t time.Time, b []byte) []byte {
+	return t.AppendFormat(b, time.RFC3339Nano)
+}

+ 15 - 0
codec/goversion_fmt_time_lt_go15.go

@@ -0,0 +1,15 @@
+// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
+// Use of this source code is governed by a MIT license found in the LICENSE file.
+
+// +build !go1.5
+
+package codec
+
+import "time"
+
+func fmtTime(t time.Time, b []byte) []byte {
+	s := t.Format(time.RFC3339Nano)
+	b = b[:len(s)]
+	copy(b, s)
+	return b
+}

+ 2 - 1
codec/json.go

@@ -439,7 +439,8 @@ func (e *jsonEncDriver) EncodeTime(t time.Time) {
 		e.EncodeNil()
 	} else {
 		e.b[0] = '"'
-		b := t.AppendFormat(e.b[1:1], time.RFC3339Nano)
+		// b := t.AppendFormat(e.b[1:1], time.RFC3339Nano)
+		b := fmtTime(t, e.b[1:1])
 		e.b[len(b)+1] = '"'
 		e.w.writeb(e.b[:len(b)+2])
 	}