json-rpc-test-server: add a "date" method

This commit is contained in:
Přemysl Eric Janouch 2020-09-01 23:09:56 +02:00
parent f4d178b3f6
commit 633f7007d1
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 23 additions and 0 deletions

View File

@ -1457,6 +1457,28 @@ json_rpc_ping (struct server_context *ctx, json_t *params)
return json_rpc_response (NULL, json_string ("pong"), NULL); return json_rpc_response (NULL, json_string ("pong"), NULL);
} }
static json_t *
json_rpc_date (struct server_context *ctx, json_t *params)
{
(void) ctx;
if (params && !json_is_null (params))
return json_rpc_response (NULL, NULL,
json_rpc_error (JSON_RPC_ERROR_INVALID_PARAMS, NULL));
time_t now = time (NULL);
const struct tm *tm = localtime (&now);
json_t *x = json_object ();
json_object_set_new (x, "year", json_integer (tm->tm_year + 1900));
json_object_set_new (x, "month", json_integer (tm->tm_mon + 1));
json_object_set_new (x, "day", json_integer (tm->tm_mday));
json_object_set_new (x, "hours", json_integer (tm->tm_hour));
json_object_set_new (x, "minutes", json_integer (tm->tm_min));
json_object_set_new (x, "seconds", json_integer (tm->tm_sec));
return json_rpc_response (NULL, x, NULL);
}
static json_t * static json_t *
process_json_rpc_request (struct server_context *ctx, json_t *request) process_json_rpc_request (struct server_context *ctx, json_t *request)
{ {
@ -1464,6 +1486,7 @@ process_json_rpc_request (struct server_context *ctx, json_t *request)
// Eventually it might be better to move this into a map in the context. // Eventually it might be better to move this into a map in the context.
static struct json_rpc_handler_info handlers[] = static struct json_rpc_handler_info handlers[] =
{ {
{ "date", json_rpc_date },
{ "ping", json_rpc_ping }, { "ping", json_rpc_ping },
}; };