From 633f7007d1e9cf6492560ed4e644f6c4d92747f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Tue, 1 Sep 2020 23:09:56 +0200 Subject: [PATCH] json-rpc-test-server: add a "date" method --- json-rpc-test-server.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/json-rpc-test-server.c b/json-rpc-test-server.c index 951a4c6..2031034 100644 --- a/json-rpc-test-server.c +++ b/json-rpc-test-server.c @@ -1457,6 +1457,28 @@ json_rpc_ping (struct server_context *ctx, json_t *params) 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 * 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. static struct json_rpc_handler_info handlers[] = { + { "date", json_rpc_date }, { "ping", json_rpc_ping }, };