15

How can I get current date / time in Lua embedded in Redis?

I need to have it in following format - YYYY-MM-DD, HH:MM:SS

Tried with os.date() but it does not recognize it.

2 Answers 2

26

Redis' Lua sandbox has only a handful of libraries, and os isn't one of these.

You can call the Redis TIME from Lua like so:

local t = redis.call('TIME')

However, you'll need to find a way to convert the epoch to the desired format and also note that it will stop you script from performing any writes (as it is a non-deterministic command).

Update: as of Redis v3.2, there is a new replication mode for scripts that is effect-based (rather than code-based). When using this mode you can actually call all the random, non-deterministic commands. More information is at EVAL's documentation page

Sign up to request clarification or add additional context in comments.

4 Comments

That's really surprise me :) never knew there is a time command
thanks to mentioned about "non-deterministic command" - this really will hurt the script. I guess I will pass date from the parameter list
That's exactly how you should do it ;)
This answer does not answer the question since there is no easy way to convert timestamp to date.
7

This was already discussed in the comments, but the correct answer should have an answer:

The current time is non-deterministic i.e. it returns different values on repeated calls. This hurts replication. For this reason, the current time should be passed into your LUA script as a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.