diff --git a/examples/README.md b/examples/README.md index eb9a0e4325f..a026bc0b1c8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,6 +10,7 @@ This folder contains example scripts showing how to use Node Redis in different | `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user | | `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch | | `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io) | +| `get-server-time.js` | Get the time from the Redis server | | `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys | | `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) | | `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes | diff --git a/examples/get-server-time.js b/examples/get-server-time.js new file mode 100644 index 00000000000..c6b7956c831 --- /dev/null +++ b/examples/get-server-time.js @@ -0,0 +1,16 @@ +// Get the time from the Redis Server. + +import { createClient } from 'redis'; + +async function getServerTime() { + const client = createClient(); + await client.connect(); + + const serverTime = await client.time(); + // 2022-02-25T12:57:40.000Z { microseconds: 351346 } + console.log(serverTime); + + await client.quit(); +} + +getServerTime(); diff --git a/packages/client/lib/commands/TIME.ts b/packages/client/lib/commands/TIME.ts index 4579845339e..1a364d6d8be 100644 --- a/packages/client/lib/commands/TIME.ts +++ b/packages/client/lib/commands/TIME.ts @@ -9,7 +9,7 @@ interface TimeReply extends Date { export function transformReply(reply: [string, string]): TimeReply { const seconds = Number(reply[0]), microseconds = Number(reply[1]), - d: Partial = new Date(seconds + Math.round(microseconds / 1000)); + d: Partial = new Date(seconds * 1000 + microseconds / 1000); d.microseconds = microseconds; return d as TimeReply; }