Kouhei Sutou
null+****@clear*****
Wed Apr 11 14:34:11 JST 2018
Kouhei Sutou 2018-04-11 14:34:11 +0900 (Wed, 11 Apr 2018) New Revision: e244388466356a33c55aa1d78d223ddf31944e87 https://github.com/groonga/groonga/commit/e244388466356a33c55aa1d78d223ddf31944e87 Message: time_format: add Added files: test/command/suite/select/function/time/time_format/iso8601.expected test/command/suite/select/function/time/time_format/iso8601.test Modified files: plugins/functions/time.c Modified: plugins/functions/time.c (+91 -1) =================================================================== --- plugins/functions/time.c 2018-04-11 14:06:53 +0900 (dc54f47ea) +++ plugins/functions/time.c 2018-04-11 14:34:11 +0900 (80dca41a4) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2016 Brazil + Copyright(C) 2016-2018 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -319,6 +319,90 @@ func_time_classify_year(grn_ctx *ctx, int n_args, grn_obj **args, GRN_TIME_CLASSIFY_UNIT_YEAR); } +static grn_obj * +func_time_format(grn_ctx *ctx, int n_args, grn_obj **args, + grn_user_data *user_data) +{ + grn_obj *time; + grn_obj *format; + + if (n_args != 2) { + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_format(): " + "wrong number of arguments (%d for 2)", + n_args); + return NULL; + } + + time = args[0]; + format = args[1]; + + if (!(time->header.type == GRN_BULK && + time->header.domain == GRN_DB_TIME)) { + grn_obj inspected; + + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, time); + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_format(): " + "the first argument must be a time: " + "<%.*s>", + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + return NULL; + } + + if (!grn_obj_is_text_family_bulk(ctx, format)) { + grn_obj inspected; + + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, format); + GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, + "time_format(): " + "the second argument must be a string: " + "<%.*s>", + (int)GRN_TEXT_LEN(&inspected), + GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + return NULL; + } + + { + int64_t time_raw; + struct tm tm; + grn_obj nul_terminated_format; + grn_obj *formatted_time; + char formatted_time_buffer[4096]; + size_t formatted_time_size; + + time_raw = GRN_TIME_VALUE(time); + if (!grn_time_to_tm(ctx, time_raw, &tm)) { + return NULL; + } + + GRN_TEXT_INIT(&nul_terminated_format, 0); + GRN_TEXT_SET(ctx, + &nul_terminated_format, + GRN_TEXT_VALUE(format), + GRN_TEXT_LEN(format)); + GRN_TEXT_PUTC(ctx, &nul_terminated_format, '\0'); + + formatted_time_size = strftime(formatted_time_buffer, + sizeof(formatted_time_buffer), + GRN_TEXT_VALUE(&nul_terminated_format), + &tm); + + formatted_time = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_TEXT, 0); + GRN_TEXT_SET(ctx, + formatted_time, + formatted_time_buffer, + formatted_time_size); + + return formatted_time; + } +} + grn_rc GRN_PLUGIN_INIT(grn_ctx *ctx) { @@ -366,6 +450,12 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx) func_time_classify_year, NULL, NULL, 0, NULL); + grn_proc_create(ctx, + "time_format", -1, + GRN_PROC_FUNCTION, + func_time_format, + NULL, NULL, 0, NULL); + return rc; } Added: test/command/suite/select/function/time/time_format/iso8601.expected (+63 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_format/iso8601.expected 2018-04-11 14:34:11 +0900 (10daf139c) @@ -0,0 +1,63 @@ +plugin_register functions/time +[[0,0.0,0.0],true] +table_create Timestamps TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +load --table Timestamps +[ +{"_key": "2016-05-05 23:59:59.999999"}, +{"_key": "2016-05-06 00:00:00.000000"}, +{"_key": "2016-05-06 00:00:00.000001"}, +{"_key": "2016-05-06 23:59:59.999999"}, +{"_key": "2016-05-07 00:00:00.000000"}, +{"_key": "2016-05-07 00:00:00.000001"} +] +[[0,0.0,0.0],6] +select Timestamps --sortby _id --limit -1 --output_columns '_key, time_format(_key, "%Y-%m-%dT%H:%M:%S%z")' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 6 + ], + [ + [ + "_key", + "Time" + ], + [ + "time_format", + null + ] + ], + [ + 1462460399.999999, + "2016-05-05T23:59:59+0900" + ], + [ + 1462460400.0, + "2016-05-06T00:00:00+0900" + ], + [ + 1462460400.000001, + "2016-05-06T00:00:00+0900" + ], + [ + 1462546799.999999, + "2016-05-06T23:59:59+0900" + ], + [ + 1462546800.0, + "2016-05-07T00:00:00+0900" + ], + [ + 1462546800.000001, + "2016-05-07T00:00:00+0900" + ] + ] + ] +] Added: test/command/suite/select/function/time/time_format/iso8601.test (+18 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/time/time_format/iso8601.test 2018-04-11 14:34:11 +0900 (447053417) @@ -0,0 +1,18 @@ +plugin_register functions/time + +table_create Timestamps TABLE_PAT_KEY Time + +load --table Timestamps +[ +{"_key": "2016-05-05 23:59:59.999999"}, +{"_key": "2016-05-06 00:00:00.000000"}, +{"_key": "2016-05-06 00:00:00.000001"}, +{"_key": "2016-05-06 23:59:59.999999"}, +{"_key": "2016-05-07 00:00:00.000000"}, +{"_key": "2016-05-07 00:00:00.000001"} +] + +select Timestamps \ + --sortby _id \ + --limit -1 \ + --output_columns '_key, time_format(_key, "%Y-%m-%dT%H:%M:%S%z")' -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180411/bd26e3d9/attachment-0001.htm