Navigation Menu

Skip to content

Commit

Permalink
time_classify_day_of_the_week: add
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 2, 2018
1 parent 7525813 commit f604294
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
62 changes: 62 additions & 0 deletions plugins/functions/time.c
Expand Up @@ -319,6 +319,63 @@ func_time_classify_year(grn_ctx *ctx, int n_args, grn_obj **args,
GRN_TIME_CLASSIFY_UNIT_YEAR);
}

static grn_obj *
func_time_classify_day_of_the_week(grn_ctx *ctx, int n_args, grn_obj **args,
grn_user_data *user_data)
{
const char *function_name = "time_classify_day_of_the_week";
grn_obj *time;

if (n_args != 1) {
GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
"%s(): "
"wrong number of arguments (%d for 1)",
function_name,
n_args);
return NULL;
}

time = args[0];
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,
"%s(): "
"the first argument must be a time: "
"<%.*s>",
function_name,
(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 *day_of_the_week;

time_raw = GRN_TIME_VALUE(time);
if (!grn_time_to_tm(ctx, time_raw, &tm)) {
return NULL;
}

day_of_the_week = grn_plugin_proc_alloc(ctx,
user_data,
GRN_DB_UINT8,
0);
if (!day_of_the_week) {
return NULL;
}
GRN_TIME_SET(ctx, day_of_the_week, tm.tm_wday);

return day_of_the_week;
}
}

static grn_obj *
func_time_format(grn_ctx *ctx, int n_args, grn_obj **args,
grn_user_data *user_data)
Expand Down Expand Up @@ -450,6 +507,11 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
GRN_PROC_FUNCTION,
func_time_classify_year,
NULL, NULL, 0, NULL);
grn_proc_create(ctx,
"time_classify_day_of_the_week", -1,
GRN_PROC_FUNCTION,
func_time_classify_day_of_the_week,
NULL, NULL, 0, NULL);

grn_proc_create(ctx,
"time_format", -1,
Expand Down
@@ -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-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"},
{"_key": "2016-05-08 23:59:59.999999"},
{"_key": "2016-05-08 00:00:00.000000"}
]
[[0,0.0,0.0],6]
select Timestamps --sortby _id --limit -1 --output_columns '_key, time_classify_day_of_the_week(_key)'
[
[
0,
0.0,
0.0
],
[
[
[
6
],
[
[
"_key",
"Time"
],
[
"time_classify_day_of_the_week",
null
]
],
[
1462460400.000001,
5
],
[
1462546799.999999,
5
],
[
1462546800.0,
6
],
[
1462546800.000001,
6
],
[
1462719599.999999,
0
],
[
1462633200.0,
0
]
]
]
]
@@ -0,0 +1,18 @@
plugin_register functions/time

table_create Timestamps TABLE_PAT_KEY Time

load --table Timestamps
[
{"_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"},
{"_key": "2016-05-08 23:59:59.999999"},
{"_key": "2016-05-08 00:00:00.000000"}
]

select Timestamps \
--sortby _id \
--limit -1 \
--output_columns '_key, time_classify_day_of_the_week(_key)'

0 comments on commit f604294

Please sign in to comment.