mirror of https://github.com/go-redis/redis.git
Merge pull request #1102 from go-redis/fix/time-helper
Marshal time as RFC3339. Add StringCmd.Time helper.
This commit is contained in:
commit
802fee4577
|
@ -3,11 +3,12 @@
|
||||||
## v7 WIP
|
## v7 WIP
|
||||||
|
|
||||||
- WrapProcess is replaced with more convenient AddHook that has access to context.Context.
|
- WrapProcess is replaced with more convenient AddHook that has access to context.Context.
|
||||||
- WithContext no longer creates shallow copy.
|
- WithContext now can not be used to create a shallow copy of the client.
|
||||||
- New methods ProcessContext, DoContext, and ExecContext.
|
- New methods ProcessContext, DoContext, and ExecContext.
|
||||||
- Client respects Context.Deadline when setting net.Conn deadline.
|
- Client respects Context.Deadline when setting net.Conn deadline.
|
||||||
- Client listens on Context.Done while waiting for a connection from the pool.
|
- Client listens on Context.Done while waiting for a connection from the pool and returns an error when context context is cancelled.
|
||||||
- Add PubSub.ChannelWithSubscriptions that sends `*Subscription` in addition to `*Message` to allow detecting reconnections.
|
- Add PubSub.ChannelWithSubscriptions that sends `*Subscription` in addition to `*Message` to allow detecting reconnections.
|
||||||
|
- `time.Time` is now marshalled in RFC3339 format. `rdb.Get("foo").Time()` helper is added to parse time.
|
||||||
|
|
||||||
## v6.15
|
## v6.15
|
||||||
|
|
||||||
|
|
|
@ -659,6 +659,13 @@ func (cmd *StringCmd) Float64() (float64, error) {
|
||||||
return strconv.ParseFloat(cmd.Val(), 64)
|
return strconv.ParseFloat(cmd.Val(), 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *StringCmd) Time() (time.Time, error) {
|
||||||
|
if cmd.err != nil {
|
||||||
|
return time.Time{}, cmd.err
|
||||||
|
}
|
||||||
|
return time.Parse(time.RFC3339, cmd.Val())
|
||||||
|
}
|
||||||
|
|
||||||
func (cmd *StringCmd) Scan(val interface{}) error {
|
func (cmd *StringCmd) Scan(val interface{}) error {
|
||||||
if cmd.err != nil {
|
if cmd.err != nil {
|
||||||
return cmd.err
|
return cmd.err
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package redis_test
|
package redis_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
|
@ -67,4 +69,19 @@ var _ = Describe("Cmd", func() {
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(val).To(Equal(f))
|
Expect(val).To(Equal(f))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("supports time.Time", func() {
|
||||||
|
tm := time.Date(2019, 01, 01, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
err := client.Set("time_key", tm, 0).Err()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
s, err := client.Get("time_key").Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(s).To(Equal("2019-01-01T00:00:00Z"))
|
||||||
|
|
||||||
|
tm2, err := client.Get("time_key").Time()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(tm2).To(BeTemporally("==", tm))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,6 +2,7 @@ package proto_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -12,6 +13,14 @@ import (
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MyType struct{}
|
||||||
|
|
||||||
|
var _ encoding.BinaryMarshaler = (*MyType)(nil)
|
||||||
|
|
||||||
|
func (t *MyType) MarshalBinary() ([]byte, error) {
|
||||||
|
return []byte("hello"), nil
|
||||||
|
}
|
||||||
|
|
||||||
var _ = Describe("WriteBuffer", func() {
|
var _ = Describe("WriteBuffer", func() {
|
||||||
var buf *bytes.Buffer
|
var buf *bytes.Buffer
|
||||||
var wr *proto.Writer
|
var wr *proto.Writer
|
||||||
|
@ -45,16 +54,25 @@ var _ = Describe("WriteBuffer", func() {
|
||||||
"\r\n")))
|
"\r\n")))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("should append marshalable args", func() {
|
It("should append time", func() {
|
||||||
err := wr.WriteArgs([]interface{}{time.Unix(1414141414, 0)})
|
err := wr.WriteArgs([]interface{}{time.Unix(1414141414, 0).UTC()})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
err = wr.Flush()
|
err = wr.Flush()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
Expect(buf.Len()).To(Equal(26))
|
Expect(buf.Len()).To(Equal(31))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("should append marshalable args", func() {
|
||||||
|
err := wr.WriteArgs([]interface{}{&MyType{}})
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
err = wr.Flush()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
Expect(buf.Len()).To(Equal(15))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
func BenchmarkWriteBuffer_Append(b *testing.B) {
|
func BenchmarkWriteBuffer_Append(b *testing.B) {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/internal/util"
|
"github.com/go-redis/redis/internal/util"
|
||||||
)
|
)
|
||||||
|
@ -92,6 +93,8 @@ func (w *Writer) writeArg(v interface{}) error {
|
||||||
} else {
|
} else {
|
||||||
return w.int(0)
|
return w.int(0)
|
||||||
}
|
}
|
||||||
|
case time.Time:
|
||||||
|
return w.string(v.Format(time.RFC3339))
|
||||||
case encoding.BinaryMarshaler:
|
case encoding.BinaryMarshaler:
|
||||||
b, err := v.MarshalBinary()
|
b, err := v.MarshalBinary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue