Kouhei Sutou
null+****@clear*****
Thu May 5 22:31:33 JST 2016
Kouhei Sutou 2016-05-05 22:31:33 +0900 (Thu, 05 May 2016) New Revision: bcff34f500c7bdd636b98899b6d59939efd6d009 https://github.com/groonga/groonga/commit/bcff34f500c7bdd636b98899b6d59939efd6d009 Message: Add grn_time_from_tm() Modified files: include/groonga/time.h lib/time.c test/unit/core/test-time.c Modified: include/groonga/time.h (+3 -0) =================================================================== --- include/groonga/time.h 2016-05-05 22:11:44 +0900 (e348ec5) +++ include/groonga/time.h 2016-05-05 22:31:33 +0900 (dd242ba) @@ -38,6 +38,9 @@ GRN_API void grn_time_now(grn_ctx *ctx, grn_obj *obj); GRN_API grn_bool grn_time_to_tm(grn_ctx *ctx, int64_t time, struct tm *tm); +GRN_API grn_bool grn_time_from_tm(grn_ctx *ctx, + int64_t *time, + struct tm *tm); #ifdef __cplusplus } Modified: lib/time.c (+39 -0) =================================================================== --- lib/time.c 2016-05-05 22:11:44 +0900 (70c420d) +++ lib/time.c 2016-05-05 22:31:33 +0900 (38fa508) @@ -130,6 +130,45 @@ grn_time_to_tm(grn_ctx *ctx, int64_t time, struct tm *tm) return grn_time_t_to_tm(ctx, sec, tm); } +static grn_bool +grn_time_t_from_tm(grn_ctx *ctx, time_t *time, struct tm *tm) +{ + grn_bool success; + + tm->tm_yday = -1; + *time = mktime(tm); + success = (tm->tm_yday != -1); + if (!success) { + ERR(GRN_INVALID_ARGUMENT, + "mktime: failed to convert struct tm to time_t: " + "<%04d-%02d-%02dT%02d:%02d:%02d>(%d)", + 1900 + tm->tm_year, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec, + tm->tm_isdst); + } + return success; +} + +grn_bool +grn_time_from_tm(grn_ctx *ctx, int64_t *time, struct tm *tm) +{ + time_t sec_time_t; + int64_t sec; + int32_t usec = 0; + + if (!grn_time_t_from_tm(ctx, &sec_time_t, tm)) { + return GRN_FALSE; + } + + sec = sec_time_t; + *time = GRN_TIME_PACK(sec, usec); + return GRN_TRUE; +} + grn_rc grn_timeval2str(grn_ctx *ctx, grn_timeval *tv, char *buf, size_t buf_size) { Modified: test/unit/core/test-time.c (+20 -0) =================================================================== --- test/unit/core/test-time.c 2016-05-05 22:11:44 +0900 (91fa86e) +++ test/unit/core/test-time.c 2016-05-05 22:31:33 +0900 (5e9612b) @@ -26,6 +26,7 @@ #include "../lib/grn-assertions.h" void test_to_tm(void); +void test_from_tm(void); static grn_ctx *context; @@ -62,3 +63,22 @@ test_to_tm(void) tm.tm_min, tm.tm_sec)); } + +void +test_from_tm(void) +{ + int64_t time; + struct tm tm; + + tm.tm_year = 116; + tm.tm_mon = 4; + tm.tm_mday = 5; + tm.tm_hour = 21; + tm.tm_min = 58; + tm.tm_sec = 49; + tm.tm_isdst = -1; + + cut_assert_true(grn_time_from_tm(context, &time, &tm)); + gcut_assert_equal_int64(1462453129000000, + time); +} -------------- next part -------------- HTML����������������������������...Download