Skip to content

Commit 0e7fb3b

Browse files
committed
Marshal time as RFC3339. Add StringCmd.Time helper.
1 parent 6bc7daa commit 0e7fb3b

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
## v7 WIP
44

55
- WrapProcess is replaced with more convenient AddHook that has access to context.Context.
6-
- WithContext no longer creates shallow copy.
6+
- WithContext now can not be used to create a shallow copy of the client.
77
- New methods ProcessContext, DoContext, and ExecContext.
88
- Client respects Context.Deadline when setting net.Conn deadline.
9-
- Client listens on Context.Done while waiting for a connection from the pool.
9+
- Client listens on Context.Done while waiting for a connection from the pool and returns an error when context context is cancelled.
1010
- Add PubSub.ChannelWithSubscriptions that sends `*Subscription` in addition to `*Message` to allow detecting reconnections.
11+
- `time.Time` is now marshalled in RFC3339 format. `rdb.Get("foo").Time()` helper is added to parse time.
1112

1213
## v6.15
1314

command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,13 @@ func (cmd *StringCmd) Float64() (float64, error) {
659659
return strconv.ParseFloat(cmd.Val(), 64)
660660
}
661661

662+
func (cmd *StringCmd) Time() (time.Time, error) {
663+
if cmd.err != nil {
664+
return time.Time{}, cmd.err
665+
}
666+
return time.Parse(time.RFC3339, cmd.Val())
667+
}
668+
662669
func (cmd *StringCmd) Scan(val interface{}) error {
663670
if cmd.err != nil {
664671
return cmd.err

command_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package redis_test
22

33
import (
4+
"time"
5+
46
"github.com/go-redis/redis"
57

68
. "github.com/onsi/ginkgo"
@@ -67,4 +69,19 @@ var _ = Describe("Cmd", func() {
6769
Expect(err).NotTo(HaveOccurred())
6870
Expect(val).To(Equal(f))
6971
})
72+
73+
It("supports time.Time", func() {
74+
tm := time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC)
75+
76+
err := client.Set("time_key", tm, 0).Err()
77+
Expect(err).NotTo(HaveOccurred())
78+
79+
s, err := client.Get("time_key").Result()
80+
Expect(err).NotTo(HaveOccurred())
81+
Expect(s).To(Equal("2019-01-01T00:00:00Z"))
82+
83+
tm2, err := client.Get("time_key").Time()
84+
Expect(err).NotTo(HaveOccurred())
85+
Expect(tm2).To(BeTemporally("==", tm))
86+
})
7087
})

internal/proto/write_buffer_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proto_test
22

33
import (
44
"bytes"
5+
"encoding"
56
"io/ioutil"
67
"testing"
78
"time"
@@ -12,6 +13,14 @@ import (
1213
. "github.com/onsi/gomega"
1314
)
1415

16+
type MyType struct{}
17+
18+
var _ encoding.BinaryMarshaler = (*MyType)(nil)
19+
20+
func (t *MyType) MarshalBinary() ([]byte, error) {
21+
return []byte("hello"), nil
22+
}
23+
1524
var _ = Describe("WriteBuffer", func() {
1625
var buf *bytes.Buffer
1726
var wr *proto.Writer
@@ -45,16 +54,25 @@ var _ = Describe("WriteBuffer", func() {
4554
"\r\n")))
4655
})
4756

48-
It("should append marshalable args", func() {
49-
err := wr.WriteArgs([]interface{}{time.Unix(1414141414, 0)})
57+
It("should append time", func() {
58+
err := wr.WriteArgs([]interface{}{time.Unix(1414141414, 0).UTC()})
5059
Expect(err).NotTo(HaveOccurred())
5160

5261
err = wr.Flush()
5362
Expect(err).NotTo(HaveOccurred())
5463

55-
Expect(buf.Len()).To(Equal(26))
64+
Expect(buf.Len()).To(Equal(31))
5665
})
5766

67+
It("should append marshalable args", func() {
68+
err := wr.WriteArgs([]interface{}{&MyType{}})
69+
Expect(err).NotTo(HaveOccurred())
70+
71+
err = wr.Flush()
72+
Expect(err).NotTo(HaveOccurred())
73+
74+
Expect(buf.Len()).To(Equal(15))
75+
})
5876
})
5977

6078
func BenchmarkWriteBuffer_Append(b *testing.B) {

internal/proto/writer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"strconv"
9+
"time"
910

1011
"github.com/go-redis/redis/internal/util"
1112
)
@@ -92,6 +93,8 @@ func (w *Writer) writeArg(v interface{}) error {
9293
} else {
9394
return w.int(0)
9495
}
96+
case time.Time:
97+
return w.string(v.Format(time.RFC3339))
9598
case encoding.BinaryMarshaler:
9699
b, err := v.MarshalBinary()
97100
if err != nil {

0 commit comments

Comments
 (0)