[Groonga-commit] groonga/groonga at 2e7aeb8 [master] Add time_classify_day()

Back to archive index

Kouhei Sutou null+****@clear*****
Thu May 5 23:44:45 JST 2016


Kouhei Sutou	2016-05-05 23:44:45 +0900 (Thu, 05 May 2016)

  New Revision: 2e7aeb823e96fa5c235ae2a77698806afce65182
  https://github.com/groonga/groonga/commit/2e7aeb823e96fa5c235ae2a77698806afce65182

  Message:
    Add time_classify_day()

  Modified files:
    plugins/functions/time.c

  Modified: plugins/functions/time.c (+54 -8)
===================================================================
--- plugins/functions/time.c    2016-05-05 23:35:36 +0900 (d391224)
+++ plugins/functions/time.c    2016-05-05 23:44:45 +0900 (67cd789)
@@ -27,7 +27,8 @@
 typedef enum {
   GRN_TIME_CLASSIFY_UNIT_SECOND,
   GRN_TIME_CLASSIFY_UNIT_MINUTE,
-  GRN_TIME_CLASSIFY_UNIT_HOUR
+  GRN_TIME_CLASSIFY_UNIT_HOUR,
+  GRN_TIME_CLASSIFY_UNIT_DAY
 } grn_time_classify_unit;
 
 static grn_obj *
@@ -41,14 +42,37 @@ func_time_classify_raw(grn_ctx *ctx,
   grn_obj *time;
   uint32_t interval_raw = 1;
   grn_obj *classed_time;
+  grn_bool accept_interval;
 
-  if (!(n_args == 1 || n_args == 2)) {
-    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
-                     "%s(): "
-                     "wrong number of arguments (%d for 1..2)",
-                     function_name,
-                     n_args);
-    return NULL;
+  switch (unit) {
+  case GRN_TIME_CLASSIFY_UNIT_SECOND :
+  case GRN_TIME_CLASSIFY_UNIT_MINUTE :
+  case GRN_TIME_CLASSIFY_UNIT_HOUR :
+    accept_interval = GRN_TRUE;
+    break;
+  case GRN_TIME_CLASSIFY_UNIT_DAY :
+    accept_interval = GRN_FALSE;
+    break;
+  }
+
+  if (accept_interval) {
+    if (!(n_args == 1 || n_args == 2)) {
+      GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                       "%s(): "
+                       "wrong number of arguments (%d for 1..2)",
+                       function_name,
+                       n_args);
+      return NULL;
+    }
+  } else {
+    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];
@@ -120,6 +144,11 @@ func_time_classify_raw(grn_ctx *ctx,
       tm.tm_min = 0;
       tm.tm_sec = 0;
       break;
+    case GRN_TIME_CLASSIFY_UNIT_DAY :
+      tm.tm_hour = 0;
+      tm.tm_min = 0;
+      tm.tm_sec = 0;
+      break;
     }
 
     if (!grn_time_from_tm(ctx, &classed_time_raw, &tm)) {
@@ -175,6 +204,18 @@ func_time_classify_hour(grn_ctx *ctx, int n_args, grn_obj **args,
                                 GRN_TIME_CLASSIFY_UNIT_HOUR);
 }
 
+static grn_obj *
+func_time_classify_day(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_day",
+                                GRN_TIME_CLASSIFY_UNIT_DAY);
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -201,6 +242,11 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
                   GRN_PROC_FUNCTION,
                   func_time_classify_hour,
                   NULL, NULL, 0, NULL);
+  grn_proc_create(ctx,
+                  "time_classify_day", -1,
+                  GRN_PROC_FUNCTION,
+                  func_time_classify_day,
+                  NULL, NULL, 0, NULL);
 
   return rc;
 }
-------------- next part --------------
HTML����������������������������...
Download 



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