9951 explained code solutions for 126 technologies


python-redisSetting datetime in Redis


We're going to save timestamp (float) to Redis and then get it as a float and convert it to datetime object:

import redis, time
from datetime import datetime

r = redis.Redis()

r.set('dt', time.time())
r.set_response_callback('GET', float)
result = datetime.fromtimestamp(r.get('dt'))ctrl + c
import redis

import Redis module

set_response_callback

automatically convert objects from Redis

float

convert object to float

datetime.fromtimestamp

convert timestamp to datetime

result

variable will contain resulting datetime object


Usage example

import redis, time
from datetime import datetime

r = redis.Redis()

r.set('dt', time.time())
r.set_response_callback('GET', float)
print(datetime.fromtimestamp(r.get('dt')))
output
2022-11-04 13:10:30.252984