hugh
hugh

Reputation: 171

Get current time in Redis

The time command gives me a list, and I'm not able to get the first element in it using any normal list commands.

redis 127.0.0.1:6379> time

1) "1375802172"

2) "168215"

redis 127.0.0.1:6379> lrange time 0 1

(empty list or set)

Upvotes: 7

Views: 8222

Answers (3)

Alexander Farber
Alexander Farber

Reputation: 22988

My suggestion to get the Unix epoch milliseconds in a Redis Lua script would be:

EVAL "local time = redis.call('TIME'); local now = math.floor(time[1] * 1000 + time[2] / 1000); return now" 0 blah

A quick demo using "redis-cli" and Linux "date" commands:

Linux screenshot

By the way, the timezone doesn't matter for the epoch seconds or milliseconds - because it is an amount of them passed since a certain event in the year 1970.

And that amount of time would be same regardless of your location on the planet Earth (unless you are travelling with near light speed).

Upvotes: 1

Fabio Marreco
Fabio Marreco

Reputation: 2303

The previous answer is correct, TIME does not return a redis list.

However, you might be able to achieve what you are seeking using a lua script:

EVAL "return redis.call('TIME')[1]" 0 0

Upvotes: 1

Didier Spezia
Didier Spezia

Reputation: 73246

It is completely unrelated to a Redis list type. The fact that a number of list operations return a multi-bulk reply does not mean that all multi-bulk replies are Redis lists.

TIME does return a standard multi-bulk reply containing two values. The first one is the Unix epoch time, and the second the number of microseconds.

If you only need one of these values, it is up to the client program to select it.

Upvotes: 4

Related Questions