[Groonga-commit] groonga/groonga at f604294 [master] time_classify_day_of_the_week: add

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Jun 28 17:59:35 JST 2018


Kouhei Sutou	2018-06-28 17:59:35 +0900 (Thu, 28 Jun 2018)

  New Revision: f604294e789df357b7b8f0fc07c13556deefded5
  https://github.com/groonga/groonga/commit/f604294e789df357b7b8f0fc07c13556deefded5

  Message:
    time_classify_day_of_the_week: add

  Added files:
    test/command/suite/select/function/time/time_classify_day_of_the_week/valid.expected
    test/command/suite/select/function/time/time_classify_day_of_the_week/valid.test
  Modified files:
    plugins/functions/time.c

  Modified: plugins/functions/time.c (+62 -0)
===================================================================
--- plugins/functions/time.c    2018-07-02 11:50:26 +0900 (4a0525bfe)
+++ plugins/functions/time.c    2018-06-28 17:59:35 +0900 (4065f6e2f)
@@ -320,6 +320,63 @@ func_time_classify_year(grn_ctx *ctx, int n_args, grn_obj **args,
 }
 
 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)
 {
@@ -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,

  Added: test/command/suite/select/function/time/time_classify_day_of_the_week/valid.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_day_of_the_week/valid.expected    2018-06-28 17:59:35 +0900 (e1816ed94)
@@ -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
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_day_of_the_week/valid.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_day_of_the_week/valid.test    2018-06-28 17:59:35 +0900 (f5e4ad2d1)
@@ -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)'
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180628/fec25783/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index