[Groonga-commit] groonga/groonga at 8c0d706 [master] Add grn_time_to_tm()

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Thu May 5 22:01:35 JST 2016


Kouhei Sutou	2016-05-05 22:01:35 +0900 (Thu, 05 May 2016)

  New Revision: 8c0d706b234f0e24b49729363f61908b4ea734c8
  https://github.com/groonga/groonga/commit/8c0d706b234f0e24b49729363f61908b4ea734c8

  Message:
    Add grn_time_to_tm()

  Added files:
    test/unit/core/test-time.c
  Copied files:
    include/groonga/time.h
      (from include/groonga.h)
  Modified files:
    include/groonga.h
    include/groonga/Makefile.am
    include/groonga/groonga.h
    lib/time.c
    test/unit/core/Makefile.am

  Modified: include/groonga.h (+1 -0)
===================================================================
--- include/groonga.h    2016-05-05 21:16:37 +0900 (cc6dbd5)
+++ include/groonga.h    2016-05-05 22:01:35 +0900 (229c275)
@@ -36,6 +36,7 @@
 #include "groonga/request_canceler.h"
 #include "groonga/request_timer.h"
 #include "groonga/thread.h"
+#include "groonga/time.h"
 #include "groonga/type.h"
 #include "groonga/util.h"
 #include "groonga/windows.h"

  Modified: include/groonga/Makefile.am (+1 -0)
===================================================================
--- include/groonga/Makefile.am    2016-05-05 21:16:37 +0900 (1f03d94)
+++ include/groonga/Makefile.am    2016-05-05 22:01:35 +0900 (35834fc)
@@ -20,6 +20,7 @@ groonga_include_HEADERS =			\
 	request_timer.h				\
 	scorer.h				\
 	thread.h				\
+	time.h					\
 	token.h					\
 	tokenizer.h				\
 	token_filter.h				\

  Modified: include/groonga/groonga.h (+0 -11)
===================================================================
--- include/groonga/groonga.h    2016-05-05 21:16:37 +0900 (e5f9202)
+++ include/groonga/groonga.h    2016-05-05 22:01:35 +0900 (9a06a79)
@@ -1566,17 +1566,6 @@ GRN_API void grn_ctx_recv_handler_set(grn_ctx *,
                       (offset) * sizeof(grn_obj *), sizeof(grn_obj *));\
 } while (0)
 
-#define GRN_TIME_USEC_PER_SEC 1000000
-#define GRN_TIME_PACK(sec, usec) ((long long int)(sec) * GRN_TIME_USEC_PER_SEC + (usec))
-#define GRN_TIME_UNPACK(time_value, sec, usec) do {\
-  sec = (time_value) / GRN_TIME_USEC_PER_SEC;\
-  usec = (time_value) % GRN_TIME_USEC_PER_SEC;\
-} while (0)
-
-GRN_API void grn_time_now(grn_ctx *ctx, grn_obj *obj);
-
-#define GRN_TIME_NOW(ctx,obj) (grn_time_now((ctx), (obj)))
-
 #define GRN_BOOL_VALUE(obj) (*((unsigned char *)GRN_BULK_HEAD(obj)))
 #define GRN_INT8_VALUE(obj) (*((signed char *)GRN_BULK_HEAD(obj)))
 #define GRN_UINT8_VALUE(obj) (*((unsigned char *)GRN_BULK_HEAD(obj)))

  Copied: include/groonga/time.h (+25 -23) 53%
===================================================================
--- include/groonga.h    2016-05-05 21:16:37 +0900 (cc6dbd5)
+++ include/groonga/time.h    2016-05-05 22:01:35 +0900 (23edb50)
@@ -1,5 +1,5 @@
 /*
-  Copyright(C) 2014-2016 Brazil
+  Copyright(C) 2016 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -18,25 +18,27 @@
 
 #pragma once
 
-#include "groonga/portability.h"
-#include "groonga/groonga.h"
-
-#include "groonga/array.h"
-#include "groonga/config.h"
-#include "groonga/dat.h"
-#include "groonga/dump.h"
-#include "groonga/expr.h"
-#include "groonga/file_reader.h"
-#include "groonga/geo.h"
-#include "groonga/hash.h"
-#include "groonga/ii.h"
-#include "groonga/obj.h"
-#include "groonga/output.h"
-#include "groonga/pat.h"
-#include "groonga/request_canceler.h"
-#include "groonga/request_timer.h"
-#include "groonga/thread.h"
-#include "groonga/type.h"
-#include "groonga/util.h"
-#include "groonga/windows.h"
-#include "groonga/windows_event_logger.h"
+#include <time.h>
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+#define GRN_TIME_USEC_PER_SEC 1000000
+#define GRN_TIME_PACK(sec, usec) ((long long int)(sec) * GRN_TIME_USEC_PER_SEC + (usec))
+#define GRN_TIME_UNPACK(time_value, sec, usec) do {\
+  sec = (time_value) / GRN_TIME_USEC_PER_SEC;\
+  usec = (time_value) % GRN_TIME_USEC_PER_SEC;\
+} while (0)
+
+GRN_API void grn_time_now(grn_ctx *ctx, grn_obj *obj);
+
+#define GRN_TIME_NOW(ctx,obj) (grn_time_now((ctx), (obj)))
+
+GRN_API grn_bool grn_time_to_tm(grn_ctx *ctx,
+                                int64_t time,
+                                struct tm *tm);
+
+#ifdef __cplusplus
+}
+#endif

  Modified: lib/time.c (+40 -12)
===================================================================
--- lib/time.c    2016-05-05 21:16:37 +0900 (70bbdd0)
+++ lib/time.c    2016-05-05 22:01:35 +0900 (70c420d)
@@ -76,30 +76,58 @@ grn_time_now(grn_ctx *ctx, grn_obj *obj)
                                        GRN_TIME_NSEC_TO_USEC(tv.tv_nsec)));
 }
 
-struct tm *
-grn_timeval2tm(grn_ctx *ctx, grn_timeval *tv, struct tm *tm_buffer)
+static grn_bool
+grn_time_t_to_tm(grn_ctx *ctx, const time_t time, struct tm *tm)
 {
-  struct tm *ltm;
+  grn_bool success;
   const char *function_name;
 #ifdef HAVE__LOCALTIME64_S
-  time_t t = tv->tv_sec;
   function_name = "localtime_s";
-  ltm = (localtime_s(tm_buffer, &t) == 0) ? tm_buffer : NULL;
+  success = (localtime_s(tm, &time) == 0);
 #else /* HAVE__LOCALTIME64_S */
 # ifdef HAVE_LOCALTIME_R
-  time_t t = tv->tv_sec;
   function_name = "localtime_r";
-  ltm = localtime_r(&t, tm_buffer);
+  success = (localtime_r(&time, tm) != NULL);
 # else /* HAVE_LOCALTIME_R */
-  time_t tvsec = (time_t) tv->tv_sec;
   function_name = "localtime";
-  ltm = localtime(&tvsec);
+  {
+    struct tm *local_tm;
+    local_tm = localtime(&time);
+    if (local_tm) {
+      success = GRN_TRUE;
+      memcpy(tm, local_tm, sizeof(struct tm));
+    } else {
+      success = GRN_FALSE;
+    }
+  }
 # endif /* HAVE_LOCALTIME_R */
 #endif /* HAVE__LOCALTIME64_S */
-  if (!ltm) {
-    SERR("%s", function_name);
+  if (!success) {
+    SERR("%s: failed to convert time_t to struct tm: <%" GRN_FMT_INT64D ">",
+         function_name,
+         (int64_t)time);
   }
-  return ltm;
+  return success;
+}
+
+struct tm *
+grn_timeval2tm(grn_ctx *ctx, grn_timeval *tv, struct tm *tm)
+{
+  if (grn_time_t_to_tm(ctx, tv->tv_sec, tm)) {
+    return tm;
+  } else {
+    return NULL;
+  }
+}
+
+grn_bool
+grn_time_to_tm(grn_ctx *ctx, int64_t time, struct tm *tm)
+{
+  int64_t sec;
+  int32_t usec;
+
+  GRN_TIME_UNPACK(time, sec, usec);
+  return grn_time_t_to_tm(ctx, sec, tm);
 }
 
 grn_rc

  Modified: test/unit/core/Makefile.am (+3 -1)
===================================================================
--- test/unit/core/Makefile.am    2016-05-05 21:16:37 +0900 (ffa7853)
+++ test/unit/core/Makefile.am    2016-05-05 22:01:35 +0900 (2efd175)
@@ -67,7 +67,8 @@ noinst_LTLIBRARIES =				\
 	test-uvector.la				\
 	test-operator.la			\
 	test-config.la				\
-	test-type.la
+	test-type.la				\
+	test-time.la
 endif
 
 AM_CPPFLAGS =			\
@@ -160,3 +161,4 @@ test_uvector_la_SOURCES			= test-uvector.c
 test_operator_la_SOURCES		= test-operator.c
 test_config_la_SOURCES			= test-config.c
 test_type_la_SOURCES			= test-type.c
+test_time_la_SOURCES			= test-time.c

  Added: test/unit/core/test-time.c (+64 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-time.c    2016-05-05 22:01:35 +0900 (91fa86e)
@@ -0,0 +1,64 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+  Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License version 2.1 as published by the Free Software Foundation.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "../../../config.h"
+
+#include <groonga.h>
+
+#include <gcutter.h>
+#include <glib/gstdio.h>
+
+#include "../lib/grn-assertions.h"
+
+void test_to_tm(void);
+
+static grn_ctx *context;
+
+void
+cut_setup(void)
+{
+  context = g_new0(grn_ctx, 1);
+  grn_ctx_init(context, 0);
+}
+
+void
+cut_teardown(void)
+{
+  if (context) {
+    grn_ctx_fin(context);
+    g_free(context);
+  }
+}
+
+void
+test_to_tm(void)
+{
+  int64_t time;
+  struct tm tm;
+
+  time = GRN_TIME_PACK(1462453129, 997984);
+  cut_assert_true(grn_time_to_tm(context, time, &tm));
+  cut_assert_equal_string("2016-05-05T21:58:49",
+                          cut_take_printf("%04d-%02d-%02dT%02d:%02d:%02d",
+                                          1900 + tm.tm_year,
+                                          tm.tm_mon + 1,
+                                          tm.tm_mday,
+                                          tm.tm_hour,
+                                          tm.tm_min,
+                                          tm.tm_sec));
+}
-------------- next part --------------
HTML����������������������������...
Download 



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