[Groonga-commit] groonga/groonga at 57c3a31 [master] Add time_classify_week()

Back to archive index

Kouhei Sutou null+****@clear*****
Fri May 6 00:11:48 JST 2016


Kouhei Sutou	2016-05-06 00:11:48 +0900 (Fri, 06 May 2016)

  New Revision: 57c3a31f2bffc3db7f91f312b4dc5b68d043e2a5
  https://github.com/groonga/groonga/commit/57c3a31f2bffc3db7f91f312b4dc5b68d043e2a5

  Message:
    Add time_classify_week()

  Added files:
    test/command/suite/select/function/time/time_classify_day/valid.expected
    test/command/suite/select/function/time/time_classify_day/valid.test
    test/command/suite/select/function/time/time_classify_week/not_underflowed.expected
    test/command/suite/select/function/time/time_classify_week/not_underflowed.test
    test/command/suite/select/function/time/time_classify_week/underflow_month.expected
    test/command/suite/select/function/time/time_classify_week/underflow_month.test
    test/command/suite/select/function/time/time_classify_week/underflow_year.expected
    test/command/suite/select/function/time/time_classify_week/underflow_year.test
  Modified files:
    plugins/functions/time.c

  Modified: plugins/functions/time.c (+48 -1)
===================================================================
--- plugins/functions/time.c    2016-05-05 23:44:45 +0900 (67cd789)
+++ plugins/functions/time.c    2016-05-06 00:11:48 +0900 (b681568)
@@ -28,7 +28,8 @@ typedef enum {
   GRN_TIME_CLASSIFY_UNIT_SECOND,
   GRN_TIME_CLASSIFY_UNIT_MINUTE,
   GRN_TIME_CLASSIFY_UNIT_HOUR,
-  GRN_TIME_CLASSIFY_UNIT_DAY
+  GRN_TIME_CLASSIFY_UNIT_DAY,
+  GRN_TIME_CLASSIFY_UNIT_WEEK
 } grn_time_classify_unit;
 
 static grn_obj *
@@ -51,6 +52,7 @@ func_time_classify_raw(grn_ctx *ctx,
     accept_interval = GRN_TRUE;
     break;
   case GRN_TIME_CLASSIFY_UNIT_DAY :
+  case GRN_TIME_CLASSIFY_UNIT_WEEK :
     accept_interval = GRN_FALSE;
     break;
   }
@@ -149,6 +151,34 @@ func_time_classify_raw(grn_ctx *ctx,
       tm.tm_min = 0;
       tm.tm_sec = 0;
       break;
+    case GRN_TIME_CLASSIFY_UNIT_WEEK :
+      if ((tm.tm_mday - tm.tm_wday) >= 0) {
+        tm.tm_mday -= tm.tm_wday;
+      } else {
+        int n_underflowed_mday = -(tm.tm_mday - tm.tm_wday);
+        int mday;
+        int max_mday = 31;
+
+        if (tm.tm_mon == 0) {
+          tm.tm_year--;
+          tm.tm_mon = 11;
+        } else {
+          tm.tm_mon--;
+        }
+
+        for (mday = max_mday; mday > n_underflowed_mday; mday--) {
+          int64_t unused;
+          tm.tm_mday = mday;
+          if (grn_time_from_tm(ctx, &unused, &tm)) {
+            break;
+          }
+        }
+        tm.tm_mday -= n_underflowed_mday;
+      }
+      tm.tm_hour = 0;
+      tm.tm_min = 0;
+      tm.tm_sec = 0;
+      break;
     }
 
     if (!grn_time_from_tm(ctx, &classed_time_raw, &tm)) {
@@ -216,6 +246,18 @@ func_time_classify_day(grn_ctx *ctx, int n_args, grn_obj **args,
                                 GRN_TIME_CLASSIFY_UNIT_DAY);
 }
 
+static grn_obj *
+func_time_classify_week(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_week",
+                                GRN_TIME_CLASSIFY_UNIT_WEEK);
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -247,6 +289,11 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
                   GRN_PROC_FUNCTION,
                   func_time_classify_day,
                   NULL, NULL, 0, NULL);
+  grn_proc_create(ctx,
+                  "time_classify_week", -1,
+                  GRN_PROC_FUNCTION,
+                  func_time_classify_week,
+                  NULL, NULL, 0, NULL);
 
   return rc;
 }

  Added: test/command/suite/select/function/time/time_classify_day/valid.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_day/valid.expected    2016-05-06 00:11:48 +0900 (6491639)
@@ -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 23:59:59.999999"},
+{"_key": "2016-05-06 00:00:00.000000"},
+{"_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"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_day(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_day",
+          "null"
+        ]
+      ],
+      [
+        1462460399.999999,
+        1462374000.0
+      ],
+      [
+        1462460400.0,
+        1462460400.0
+      ],
+      [
+        1462460400.000001,
+        1462460400.0
+      ],
+      [
+        1462546799.999999,
+        1462460400.0
+      ],
+      [
+        1462546800.0,
+        1462546800.0
+      ],
+      [
+        1462546800.000001,
+        1462546800.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_day/valid.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_day/valid.test    2016-05-06 00:11:48 +0900 (ec408c4)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2016-05-05 23:59:59.999999"},
+{"_key": "2016-05-06 00:00:00.000000"},
+{"_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"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_day(_key)'

  Added: test/command/suite/select/function/time/time_classify_week/not_underflowed.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_week/not_underflowed.expected    2016-05-06 00:11:48 +0900 (c22f9f1)
@@ -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-07 23:59:59.999999"},
+{"_key": "2016-05-08 00:00:00.000000"},
+{"_key": "2016-05-08 00:00:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_week(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_week",
+          "null"
+        ]
+      ],
+      [
+        1462028399.999999,
+        1461423600.0
+      ],
+      [
+        1462028400.0,
+        1462028400.0
+      ],
+      [
+        1462028400.000001,
+        1462028400.0
+      ],
+      [
+        1462633199.999999,
+        1462028400.0
+      ],
+      [
+        1462633200.0,
+        1462633200.0
+      ],
+      [
+        1462633200.000001,
+        1462633200.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_week/not_underflowed.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_week/not_underflowed.test    2016-05-06 00:11:48 +0900 (34e955f)
@@ -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-07 23:59:59.999999"},
+{"_key": "2016-05-08 00:00:00.000000"},
+{"_key": "2016-05-08 00:00:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_week(_key)'

  Added: test/command/suite/select/function/time/time_classify_week/underflow_month.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_week/underflow_month.expected    2016-05-06 00:11:48 +0900 (e599043)
@@ -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-01-30 23:59:59.999999"},
+{"_key": "2016-01-31 00:00:00.000000"},
+{"_key": "2016-01-31 00:00:00.000001"},
+{"_key": "2016-02-06 23:59:59.999999"},
+{"_key": "2016-02-07 00:00:00.000000"},
+{"_key": "2016-02-07 00:00:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_week(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_week",
+          "null"
+        ]
+      ],
+      [
+        1454165999.999999,
+        1453561200.0
+      ],
+      [
+        1454166000.0,
+        1454166000.0
+      ],
+      [
+        1454166000.000001,
+        1454166000.0
+      ],
+      [
+        1454770799.999999,
+        1454166000.0
+      ],
+      [
+        1454770800.0,
+        1454770800.0
+      ],
+      [
+        1454770800.000001,
+        1454770800.0
+      ]
+    ]
+  ]
+]

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

  Added: test/command/suite/select/function/time/time_classify_week/underflow_year.expected (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_week/underflow_year.expected    2016-05-06 00:11:48 +0900 (476a24f)
@@ -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": "2015-12-26 23:59:59.999999"},
+{"_key": "2015-12-27 00:00:00.000000"},
+{"_key": "2015-12-27 00:00:00.000001"},
+{"_key": "2016-01-02 23:59:59.999999"},
+{"_key": "2016-01-03 00:00:00.000000"},
+{"_key": "2016-01-03 00:00:00.000001"}
+]
+[[0,0.0,0.0],6]
+select Timestamps   --sortby _id   --limit -1   --output_columns '_key, time_classify_week(_key)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        6
+      ],
+      [
+        [
+          "_key",
+          "Time"
+        ],
+        [
+          "time_classify_week",
+          "null"
+        ]
+      ],
+      [
+        1451141999.999999,
+        1450537200.0
+      ],
+      [
+        1451142000.0,
+        1451142000.0
+      ],
+      [
+        1451142000.000001,
+        1451142000.0
+      ],
+      [
+        1451746799.999999,
+        1451142000.0
+      ],
+      [
+        1451746800.0,
+        1451746800.0
+      ],
+      [
+        1451746800.000001,
+        1451746800.0
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/time/time_classify_week/underflow_year.test (+18 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/time/time_classify_week/underflow_year.test    2016-05-06 00:11:48 +0900 (b27fbd0)
@@ -0,0 +1,18 @@
+plugin_register functions/time
+
+table_create Timestamps TABLE_PAT_KEY Time
+
+load --table Timestamps
+[
+{"_key": "2015-12-26 23:59:59.999999"},
+{"_key": "2015-12-27 00:00:00.000000"},
+{"_key": "2015-12-27 00:00:00.000001"},
+{"_key": "2016-01-02 23:59:59.999999"},
+{"_key": "2016-01-03 00:00:00.000000"},
+{"_key": "2016-01-03 00:00:00.000001"}
+]
+
+select Timestamps \
+  --sortby _id \
+  --limit -1 \
+  --output_columns '_key, time_classify_week(_key)'
-------------- next part --------------
HTML����������������������������...
다운로드 



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