diff --git a/client/ledis-py/ledis/client.py b/client/ledis-py/ledis/client.py index 2504e5d..93245b8 100644 --- a/client/ledis-py/ledis/client.py +++ b/client/ledis-py/ledis/client.py @@ -137,6 +137,7 @@ class Ledis(object): 'PING': lambda r: nativestr(r) == 'PONG', 'SET': lambda r: r and nativestr(r) == 'OK', 'INFO': parse_info, + 'TIME': lambda x: (int(x[0]), int(x[1])), } @@ -1035,6 +1036,13 @@ class Ledis(object): def scriptflush(self): return self.execute_command('SCRIPT', 'FLUSH') + def time(self): + """ + Returns the server time as a 2-item tuple of ints: + (seconds since epoch, microseconds into this second). + """ + return self.execute_command('TIME') + class Transaction(Ledis): def __init__(self, connection_pool, response_callbacks): diff --git a/client/nodejs/ledis/lib/commands.js b/client/nodejs/ledis/lib/commands.js index 8a24f6c..5ff7868 100644 --- a/client/nodejs/ledis/lib/commands.js +++ b/client/nodejs/ledis/lib/commands.js @@ -8,6 +8,7 @@ module.exports = [ "info", "flushall", "flushdb", + "time", "bget", "bdelete", diff --git a/client/openresty/ledis.lua b/client/openresty/ledis.lua index 7834c2b..e00d1d7 100644 --- a/client/openresty/ledis.lua +++ b/client/openresty/ledis.lua @@ -153,6 +153,7 @@ local commands = { "info", "flushall", "flushdb", + "time", -- [[transaction]] "begin", diff --git a/cmd/ledis-cli/const.go b/cmd/ledis-cli/const.go index 3bca898..4d9bb6a 100644 --- a/cmd/ledis-cli/const.go +++ b/cmd/ledis-cli/const.go @@ -1,4 +1,4 @@ -//This file was generated by .tools/generate_commands.py on Thu Oct 02 2014 15:24:07 +0800 +//This file was generated by .tools/generate_commands.py on Wed Oct 08 2014 13:52:31 +0800 package main var helpCommands = [][]string{ @@ -96,6 +96,7 @@ var helpCommands = [][]string{ {"SUNIONSTORE", "destination key [key ...]", "Set"}, {"SXSCAN", "key [MATCH match] [COUNT count]", "Set"}, {"SYNC", "logid", "Replication"}, + {"TIME", "-", "Server"}, {"TTL", "key", "KV"}, {"XSCAN", "key [MATCH match] [COUNT count]", "KV"}, {"ZADD", "key score member [score member ...]", "ZSet"}, diff --git a/doc/commands.json b/doc/commands.json index b813d3b..3491c3c 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -628,6 +628,11 @@ "arguments" : "-", "group": "Script", "readonly": false - } + }, + "TIME": { + "arguments" : "-", + "group": "Server", + "readonly": true + } } diff --git a/doc/commands.md b/doc/commands.md index 2cae8fd..b71fc9b 100644 --- a/doc/commands.md +++ b/doc/commands.md @@ -132,6 +132,7 @@ Table of Contents - [FLUSHALL](#flushall) - [FLUSHDB](#flushdb) - [INFO [section]](#info-section) + - [TIME](#time) - [Transaction](#transaction) - [BEGIN](#begin) - [ROLLBACK](#rollback) @@ -2570,6 +2571,14 @@ The optional parameter can be used to select a specific section of information: When no parameter is provided, all will return. +### TIME + +The TIME command returns the current server time as a two items lists: a Unix timestamp and the amount of microseconds already elapsed in the current second + +**Return value** + +array: two elements, one is unix time in seconds, the other is microseconds. + ## Transaction ### BEGIN diff --git a/server/cmd_server.go b/server/cmd_server.go index 198003c..38e96dd 100644 --- a/server/cmd_server.go +++ b/server/cmd_server.go @@ -2,8 +2,11 @@ package server import ( "github.com/siddontang/go/hack" + "github.com/siddontang/go/num" + "strconv" "strings" + "time" ) func pingCommand(c *client) error { @@ -85,6 +88,29 @@ func flushdbCommand(c *client) error { return nil } +func timeCommand(c *client) error { + if len(c.args) != 0 { + return ErrCmdParams + } + + t := time.Now() + + //seconds + s := t.Unix() + n := t.UnixNano() + + //micro seconds + m := (n - s*1e9) / 1e3 + + ay := []interface{}{ + num.FormatInt64ToSlice(s), + num.FormatInt64ToSlice(m), + } + + c.resp.writeArray(ay) + return nil +} + func init() { register("ping", pingCommand) register("echo", echoCommand) @@ -92,4 +118,5 @@ func init() { register("info", infoCommand) register("flushall", flushallCommand) register("flushdb", flushdbCommand) + register("time", timeCommand) }