redis - C: string to milisecs timestamp -
i want return in function time_t value timestamp in string format don't it. need help.
i read string key of redis database timestamp value form, example, "1456242904.226683"
my code is:
time_t get_ts(rediscontext *ctx) { redisreply *reply; reply = rediscommand(ctx, "get %s", "key"); if(reply == null){ return -1; } char error[255]; sprintf(error, "%s", "get_ts 2:",reply->str); send_log(error); freereplyobject(reply); return reply->str; }
reply->str string value need return time_t value.
how can it?
thanks
i assume 1456242904.226683 seconds past since 00:00, jan 1 1970. 46 years. 1456242904.226683 floating point value , time_t
integral data type. can't convert 1456242904.226683 time_t
exactly, can convert 1456242904. first use atof
convert string floting point value, cast floating point value time_t
:
#include <stdlib.h> // atof time_t get_ts(rediscontext *ctx) { redisreply *reply; reply = rediscommand(ctx, "get %s", "key"); if(reply == null){ return -1; } char error[255]; sprintf(error, "%s", "get_ts 2:",reply->str); send_log(error); time_t t = (time_t)atof(reply->str); // ^^^^^^ ^^^^ freereplyobject(reply); return t; }
Comments
Post a Comment