[Groonga-commit] groonga/groonga at 1651d11 [master] Add time_classify_month()

Back to archive index

Kouhei Sutou null+****@clear*****
Fri May 6 00:17:19 JST 2016


Kouhei Sutou	2016-05-06 00:17:19 +0900 (Fri, 06 May 2016)

  New Revision: 1651d11bccd16bc9db417af6490b5a07661993e2
  https://github.com/groonga/groonga/commit/1651d11bccd16bc9db417af6490b5a07661993e2

  Message:
    Add time_classify_month()

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

  Modified: plugins/functions/time.c (+29 -1)
===================================================================
--- plugins/functions/time.c    2016-05-06 00:11:48 +0900 (b681568)
+++ plugins/functions/time.c    2016-05-06 00:17:19 +0900 (2e6cb92)
@@ -29,7 +29,8 @@ typedef enum {
   GRN_TIME_CLASSIFY_UNIT_MINUTE,
   GRN_TIME_CLASSIFY_UNIT_HOUR,
   GRN_TIME_CLASSIFY_UNIT_DAY,
-  GRN_TIME_CLASSIFY_UNIT_WEEK
+  GRN_TIME_CLASSIFY_UNIT_WEEK,
+  GRN_TIME_CLASSIFY_UNIT_MONTH
 } grn_time_classify_unit;
 
 static grn_obj *
@@ -55,6 +56,9 @@ func_time_classify_raw(grn_ctx *ctx,
   case GRN_TIME_CLASSIFY_UNIT_WEEK :
     accept_interval = GRN_FALSE;
     break;
+  case GRN_TIME_CLASSIFY_UNIT_MONTH :
+    accept_interval = GRN_TRUE;
+    break;
   }
 
   if (accept_interval) {
@@ -179,6 +183,13 @@ func_time_classify_raw(grn_ctx *ctx,
       tm.tm_min = 0;
       tm.tm_sec = 0;
       break;
+    case GRN_TIME_CLASSIFY_UNIT_MONTH :
+      tm.tm_mon = (tm.tm_mon / interval_raw) * interval_raw;
+      tm.tm_mday = 1;
+      tm.tm_hour = 0;
+      tm.tm_min = 0;
+      tm.tm_sec = 0;
+      break;
     }
 
     if (!grn_time_from_tm(ctx, &classed_time_raw, &tm)) {
@@ -258,6 +269,18 @@ func_time_classify_week(grn_ctx *ctx, int n_args, grn_obj **args,
                                 GRN_TIME_CLASSIFY_UNIT_WEEK);
 }
 
+static grn_obj *
+func_time_classify_month(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_month",
+                                GRN_TIME_CLASSIFY_UNIT_MONTH);
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -294,6 +317,11 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
                   GRN_PROC_FUNCTION,
                   func_time_classify_week,
                   NULL, NULL, 0, NULL);
+  grn_proc_create(ctx,
+                  "time_classify_month", -1,
+                  GRN_PROC_FUNCTION,
+                  func_time_classify_month,
+                  NULL, NULL, 0, NULL);
 
   return rc;
 }

  Added: test/command/suite/select/function/time/time_classify_month/default.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_month/default.expected    2016-05-06 00:17:19 +0900 (8ec89e3)
@@ -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-04-30 23:59:59.999999"},
+{"_key": "2016-05-01 00:00:00.000000"},
+{"_key": "2016-05-01 00:00:00.000001"},
+{"_key": "2016-05-31 23:59:59.999999"},
+{"_key": "2016-06-01 00:00:00.000000"},
+{"_key": "2016-06-01 00:00:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_month(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_month",
+          "null"
+        ]
+      ],
+      [
+        1462028399.999999,
+        1459436400.0
+      ],
+      [
+        1462028400.0,
+        1462028400.0
+      ],
+      [
+        1462028400.000001,
+        1462028400.0
+      ],
+      [
+        1464706799.999999,
+        1462028400.0
+      ],
+      [
+        1464706800.0,
+        1464706800.0
+      ],
+      [
+        1464706800.000001,
+        1464706800.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_month/default.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_month/default.test    2016-05-06 00:17:19 +0900 (d2e3036)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2016-04-30 23:59:59.999999"},
+{"_key": "2016-05-01 00:00:00.000000"},
+{"_key": "2016-05-01 00:00:00.000001"},
+{"_key": "2016-05-31 23:59:59.999999"},
+{"_key": "2016-06-01 00:00:00.000000"},
+{"_key": "2016-06-01 00:00:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_month(_key)'

  Added: test/command/suite/select/function/time/time_classify_month/interval.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_month/interval.expected    2016-05-06 00:17:19 +0900 (82584b5)
@@ -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-04-30 23:59:59.999999"},
+{"_key": "2016-05-01 00:00:00.000000"},
+{"_key": "2016-05-01 00:00:00.000001"},
+{"_key": "2016-06-30 23:59:59.999999"},
+{"_key": "2016-07-01 00:00:00.000000"},
+{"_key": "2016-07-01 00:00:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_month(_key, 2)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_month",
+          "null"
+        ]
+      ],
+      [
+        1462028399.999999,
+        1456758000.0
+      ],
+      [
+        1462028400.0,
+        1462028400.0
+      ],
+      [
+        1462028400.000001,
+        1462028400.0
+      ],
+      [
+        1467298799.999999,
+        1462028400.0
+      ],
+      [
+        1467298800.0,
+        1467298800.0
+      ],
+      [
+        1467298800.000001,
+        1467298800.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_month/interval.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_month/interval.test    2016-05-06 00:17:19 +0900 (470488b)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2016-04-30 23:59:59.999999"},
+{"_key": "2016-05-01 00:00:00.000000"},
+{"_key": "2016-05-01 00:00:00.000001"},
+{"_key": "2016-06-30 23:59:59.999999"},
+{"_key": "2016-07-01 00:00:00.000000"},
+{"_key": "2016-07-01 00:00:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_month(_key, 2)'
-------------- next part --------------
HTML����������������������������...
Download 



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