Get current time in Redis
Asked Answered
P

3

7

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)

Patency answered 6/8, 2013 at 15:41 Comment(1)
Man, did u figure out how to work it out? The answers lower didn't help meHuddleston
C
4

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.

Cutout answered 6/8, 2013 at 16:16 Comment(9)
I am new to Redis and the the documentation says a two item list is returned redis.io/commands/time Is it possible to use the Redis time e.g. as the value of a key-value pair?Patency
I'm not sure I understand the question, but you can perform a first roundtrip to get the time (TIME command), and a second to populate a key/value (SET command).Cutout
How exactly? How do I set a key or value to the current time?Patency
See the SET command in Redis documentation. It's pretty straightforward. I recommend you to go over one of the Redis tutorials out there.Puleo
Go to redis.io/commands/time - click in the example widget (you can enter commands here) - type TIME (two values V1 and V2 will be returned) - type SET mytime V1 (of course, replace V1 by the value from the previous command).Cutout
@OferZelig Sorry but I know how to use the SET command, when I said new I didn't mean I literally just started using it. "replace V1 by the value from the previous command" - you still have not told me how to do this, I don't want to copy and paste the V1 value in, I need mytime to be set to the current time automatically. Thanks for your help.Patency
You cannot do it on server-side. Even with a server-side Lua script, it is not possible (because it could not be safely replicated to a slave).Cutout
If you are new to Redis, I would recommend to have a look at: redis.io/topics/data-types-intro and openmymind.net/2012/1/23/The-Little-Redis-BookCutout
At least one would answer here, only read, look, recommend... The question is so clear, but someone said he was not sure, he understood it... shockedHuddleston
I
1

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
Impure answered 8/2, 2017 at 19:16 Comment(1)
It returns an Array reply. From documentation: 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 secondGoy
M
1

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).

Modifier answered 31/8, 2023 at 9:41 Comment(1)
helped me solve my problem. I needed to retrieve timestamp at millisecond precision in my Lua script executing in Redis 7Stroganoff

© 2022 - 2024 — McMap. All rights reserved.