[Groonga-commit] groonga/groonga at 1e0e402 [master] Add time_classify_minute()

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Thu May 5 23:30:06 JST 2016


Kouhei Sutou	2016-05-05 23:30:06 +0900 (Thu, 05 May 2016)

  New Revision: 1e0e4026ca49fdf12f13e604a0f54f3d96829fe3
  https://github.com/groonga/groonga/commit/1e0e4026ca49fdf12f13e604a0f54f3d96829fe3

  Message:
    Add time_classify_minute()

  Added files:
    test/command/suite/select/function/time/time_classify_minute/default.expected
    test/command/suite/select/function/time/time_classify_minute/default.test
    test/command/suite/select/function/time/time_classify_minute/interval.expected
    test/command/suite/select/function/time/time_classify_minute/interval.test
  Modified files:
    plugins/functions/time.c

  Modified: plugins/functions/time.c (+56 -9)
===================================================================
--- plugins/functions/time.c    2016-05-05 23:18:41 +0900 (b334175)
+++ plugins/functions/time.c    2016-05-05 23:30:06 +0900 (1a31069)
@@ -24,9 +24,18 @@
 
 #include <math.h>
 
+typedef enum {
+  GRN_TIME_CLASSIFY_UNIT_SECOND,
+  GRN_TIME_CLASSIFY_UNIT_MINUTE
+} grn_time_classify_unit;
+
 static grn_obj *
-func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
-                          grn_user_data *user_data)
+func_time_classify_raw(grn_ctx *ctx,
+                       int n_args,
+                       grn_obj **args,
+                       grn_user_data *user_data,
+                       const char *function_name,
+                       grn_time_classify_unit unit)
 {
   grn_obj *time;
   uint32_t interval_raw = 1;
@@ -34,8 +43,9 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
 
   if (!(n_args == 1 || n_args == 2)) {
     GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
-                     "time_classify_second(): "
-                     "wrong time of arguments (%d for 1..2)",
+                     "%s(): "
+                     "wrong number of arguments (%d for 1..2)",
+                     function_name,
                      n_args);
     return NULL;
   }
@@ -48,9 +58,10 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
     GRN_TEXT_INIT(&inspected, 0);
     grn_inspect(ctx, &inspected, time);
     GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
-                     "time_classify_second(): "
+                     "%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);
@@ -69,9 +80,10 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
       GRN_TEXT_INIT(&inspected, 0);
       grn_inspect(ctx, &inspected, interval);
       GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
-                       "time_classify_second(): "
+                       "%s(): "
                        "the second argument must be a number: "
                        "<%.*s>",
+                       function_name,
                        (int)GRN_TEXT_LEN(&inspected),
                        GRN_TEXT_VALUE(&inspected));
       GRN_OBJ_FIN(ctx, &inspected);
@@ -87,7 +99,6 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
   {
     int64_t time_raw;
     struct tm tm;
-    int classed_second;
     int64_t classed_time_raw;
 
     time_raw = GRN_TIME_VALUE(time);
@@ -95,8 +106,15 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
       return NULL;
     }
 
-    classed_second = (tm.tm_sec / interval_raw) * interval_raw;
-    tm.tm_sec = classed_second;
+    switch (unit) {
+    case GRN_TIME_CLASSIFY_UNIT_SECOND :
+      tm.tm_sec = (tm.tm_sec / interval_raw) * interval_raw;
+      break;
+    case GRN_TIME_CLASSIFY_UNIT_MINUTE :
+      tm.tm_min = (tm.tm_min / interval_raw) * interval_raw;
+      tm.tm_sec = 0;
+      break;
+    }
 
     if (!grn_time_from_tm(ctx, &classed_time_raw, &tm)) {
       return NULL;
@@ -115,6 +133,30 @@ func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
   }
 }
 
+static grn_obj *
+func_time_classify_second(grn_ctx *ctx, int n_args, grn_obj **args,
+                          grn_user_data *user_data)
+{
+  return func_time_classify_raw(ctx,
+                                n_args,
+                                args,
+                                user_data,
+                                "time_classify_second",
+                                GRN_TIME_CLASSIFY_UNIT_SECOND);
+}
+
+static grn_obj *
+func_time_classify_minute(grn_ctx *ctx, int n_args, grn_obj **args,
+                          grn_user_data *user_data)
+{
+  return func_time_classify_raw(ctx,
+                                n_args,
+                                args,
+                                user_data,
+                                "time_classify_minute",
+                                GRN_TIME_CLASSIFY_UNIT_MINUTE);
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -131,6 +173,11 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
                   GRN_PROC_FUNCTION,
                   func_time_classify_second,
                   NULL, NULL, 0, NULL);
+  grn_proc_create(ctx,
+                  "time_classify_minute", -1,
+                  GRN_PROC_FUNCTION,
+                  func_time_classify_minute,
+                  NULL, NULL, 0, NULL);
 
   return rc;
 }

  Added: test/command/suite/select/function/time/time_classify_minute/default.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_minute/default.expected    2016-05-05 23:30:06 +0900 (49ef35a)
@@ -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 22:32:59.999999"},
+{"_key": "2016-05-05 22:33:00.000000"},
+{"_key": "2016-05-05 22:33:00.000001"},
+{"_key": "2016-05-05 22:33:59.999999"},
+{"_key": "2016-05-05 22:34:00.000000"},
+{"_key": "2016-05-05 22:34:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_minute(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_minute",
+          "null"
+        ]
+      ],
+      [
+        1462455179.999999,
+        1462455120.0
+      ],
+      [
+        1462455180.0,
+        1462455180.0
+      ],
+      [
+        1462455180.000001,
+        1462455180.0
+      ],
+      [
+        1462455239.999999,
+        1462455180.0
+      ],
+      [
+        1462455240.0,
+        1462455240.0
+      ],
+      [
+        1462455240.000001,
+        1462455240.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_minute/default.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_minute/default.test    2016-05-05 23:30:06 +0900 (a41940a)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2016-05-05 22:32:59.999999"},
+{"_key": "2016-05-05 22:33:00.000000"},
+{"_key": "2016-05-05 22:33:00.000001"},
+{"_key": "2016-05-05 22:33:59.999999"},
+{"_key": "2016-05-05 22:34:00.000000"},
+{"_key": "2016-05-05 22:34:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_minute(_key)'

  Added: test/command/suite/select/function/time/time_classify_minute/interval.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_minute/interval.expected    2016-05-05 23:30:06 +0900 (62c4ad9)
@@ -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 22:29:59.999999"},
+{"_key": "2016-05-05 22:30:00.000000"},
+{"_key": "2016-05-05 22:30:00.000001"},
+{"_key": "2016-05-05 22:39:59.999999"},
+{"_key": "2016-05-05 22:40:00.000000"},
+{"_key": "2016-05-05 22:40:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_minute(_key, 10)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_minute",
+          "null"
+        ]
+      ],
+      [
+        1462454999.999999,
+        1462454400.0
+      ],
+      [
+        1462455000.0,
+        1462455000.0
+      ],
+      [
+        1462455000.000001,
+        1462455000.0
+      ],
+      [
+        1462455599.999999,
+        1462455000.0
+      ],
+      [
+        1462455600.0,
+        1462455600.0
+      ],
+      [
+        1462455600.000001,
+        1462455600.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_minute/interval.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_minute/interval.test    2016-05-05 23:30:06 +0900 (8668a54)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2016-05-05 22:29:59.999999"},
+{"_key": "2016-05-05 22:30:00.000000"},
+{"_key": "2016-05-05 22:30:00.000001"},
+{"_key": "2016-05-05 22:39:59.999999"},
+{"_key": "2016-05-05 22:40:00.000000"},
+{"_key": "2016-05-05 22:40:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_minute(_key, 10)'
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index