mirror of https://github.com/go-redis/redis.git
Add TimeCmd.
This commit is contained in:
parent
6f8957c5b7
commit
dcdf129dd5
42
command.go
42
command.go
|
@ -355,6 +355,48 @@ func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
|
|||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type TimeCmd struct {
|
||||
baseCmd
|
||||
|
||||
val time.Time
|
||||
}
|
||||
|
||||
func NewTimeCmd(args ...interface{}) *TimeCmd {
|
||||
cmd := newBaseCmd(args)
|
||||
return &TimeCmd{
|
||||
baseCmd: cmd,
|
||||
}
|
||||
}
|
||||
|
||||
func (cmd *TimeCmd) reset() {
|
||||
cmd.val = time.Time{}
|
||||
cmd.err = nil
|
||||
}
|
||||
|
||||
func (cmd *TimeCmd) Val() time.Time {
|
||||
return cmd.val
|
||||
}
|
||||
|
||||
func (cmd *TimeCmd) Result() (time.Time, error) {
|
||||
return cmd.val, cmd.err
|
||||
}
|
||||
|
||||
func (cmd *TimeCmd) String() string {
|
||||
return cmdString(cmd, cmd.val)
|
||||
}
|
||||
|
||||
func (cmd *TimeCmd) readReply(cn *pool.Conn) error {
|
||||
v, err := cn.Rd.ReadArrayReply(timeParser)
|
||||
if err != nil {
|
||||
cmd.err = err
|
||||
return err
|
||||
}
|
||||
cmd.val = v.(time.Time)
|
||||
return nil
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
type BoolCmd struct {
|
||||
baseCmd
|
||||
|
||||
|
|
|
@ -199,7 +199,7 @@ type Cmdable interface {
|
|||
ShutdownSave() *StatusCmd
|
||||
ShutdownNoSave() *StatusCmd
|
||||
SlaveOf(host, port string) *StatusCmd
|
||||
Time() *StringSliceCmd
|
||||
Time() *TimeCmd
|
||||
Eval(script string, keys []string, args ...interface{}) *Cmd
|
||||
EvalSha(sha1 string, keys []string, args ...interface{}) *Cmd
|
||||
ScriptExists(scripts ...string) *BoolSliceCmd
|
||||
|
@ -1741,9 +1741,8 @@ func (c *cmdable) Sync() {
|
|||
panic("not implemented")
|
||||
}
|
||||
|
||||
// TODO: add TimeCmd and use it here
|
||||
func (c *cmdable) Time() *StringSliceCmd {
|
||||
cmd := NewStringSliceCmd("time")
|
||||
func (c *cmdable) Time() *TimeCmd {
|
||||
cmd := NewTimeCmd("time")
|
||||
c.process(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
|
|
@ -159,9 +159,9 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
|
||||
It("should Time", func() {
|
||||
time := client.Time()
|
||||
Expect(time.Err()).NotTo(HaveOccurred())
|
||||
Expect(time.Val()).To(HaveLen(2))
|
||||
tm, err := client.Time().Result()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
|
||||
})
|
||||
|
||||
})
|
||||
|
|
31
parser.go
31
parser.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gopkg.in/redis.v5/internal/proto"
|
||||
)
|
||||
|
@ -364,6 +365,7 @@ func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
|
|||
return &cmd, nil
|
||||
}
|
||||
|
||||
// Implements proto.MultiBulkParse
|
||||
func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
|
||||
m := make(map[string]*CommandInfo, n)
|
||||
for i := int64(0); i < n; i++ {
|
||||
|
@ -377,3 +379,32 @@ func commandInfoSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
|
|||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Implements proto.MultiBulkParse
|
||||
func timeParser(rd *proto.Reader, n int64) (interface{}, error) {
|
||||
if n != 2 {
|
||||
fmt.Errorf("got %d elements, expected 2", n)
|
||||
}
|
||||
|
||||
secStr, err := rd.ReadStringReply()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
microsecStr, err := rd.ReadStringReply()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sec, err := strconv.ParseInt(secStr, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
microsec, err := strconv.ParseInt(microsecStr, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return time.Unix(sec, microsec*100), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue