| 1 | /* |
| 2 | Copyright (C) 2009-2016 Brazil |
| 3 | Copyright (C) 2020-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn_time.h" |
| 21 | #include "grn_ctx.h" |
| 22 | #include "grn_str.h" |
| 23 | |
| 24 | #include <math.h> |
| 25 | #include <stdio.h> |
| 26 | #include <time.h> |
| 27 | |
| 28 | #if defined(HAVE__LOCALTIME64_S) && defined(__GNUC__) |
| 29 | # ifdef _WIN64 |
| 30 | # define localtime_s(tm, time) _localtime64_s(tm, time) |
| 31 | # else /* _WIN64 */ |
| 32 | # define localtime_s(tm, time) _localtime32_s(tm, time) |
| 33 | # endif /* _WIN64 */ |
| 34 | #endif /* defined(HAVE__LOCALTIME64_S) && defined(__GNUC__) */ |
| 35 | |
| 36 | /* fixme by 2038 */ |
| 37 | |
| 38 | grn_rc |
| 39 | grn_timeval_now(grn_ctx *ctx, grn_timeval *tv) |
| 40 | { |
| 41 | #ifdef WIN32 |
| 42 | struct __timeb64 tb; |
| 43 | _ftime64_s(&tb); |
| 44 | tv->tv_sec = tb.time; |
| 45 | tv->tv_nsec = tb.millitm * (GRN_TIME_NSEC_PER_SEC / 1000); |
| 46 | return GRN_SUCCESS; |
| 47 | #else /* WIN32 */ |
| 48 | # ifdef HAVE_CLOCK_GETTIME |
| 49 | struct timespec t; |
| 50 | if (clock_gettime(CLOCK_REALTIME, &t)) { |
| 51 | SERR("clock_gettime"); |
| 52 | } else { |
| 53 | tv->tv_sec = t.tv_sec; |
| 54 | tv->tv_nsec = (int32_t)(t.tv_nsec); |
| 55 | } |
| 56 | return ctx->rc; |
| 57 | # else /* HAVE_CLOCK_GETTIME */ |
| 58 | struct timeval t; |
| 59 | if (gettimeofday(&t, NULL)) { |
| 60 | SERR("gettimeofday"); |
| 61 | } else { |
| 62 | tv->tv_sec = t.tv_sec; |
| 63 | tv->tv_nsec = GRN_TIME_USEC_TO_NSEC(t.tv_usec); |
| 64 | } |
| 65 | return ctx->rc; |
| 66 | # endif /* HAVE_CLOCK_GETTIME */ |
| 67 | #endif /* WIN32 */ |
| 68 | } |
| 69 | |
| 70 | grn_timeval |
| 71 | grn_timeval_from_double(grn_ctx *ctx, double value) |
| 72 | { |
| 73 | grn_timeval timeval; |
| 74 | double sec = trunc(value); |
| 75 | timeval.tv_sec = (int64_t)sec; |
| 76 | timeval.tv_nsec = |
| 77 | (int32_t)( |
| 78 | ((int64_t)((value - (double)(timeval.tv_sec)) * GRN_TIME_NSEC_PER_SEC) % |
| 79 | GRN_TIME_NSEC_PER_SEC)); |
| 80 | return timeval; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | grn_time_now(grn_ctx *ctx, grn_obj *obj) |
| 85 | { |
| 86 | grn_timeval tv; |
| 87 | grn_timeval_now(ctx, &tv); |
| 88 | GRN_TIME_SET(ctx, obj, GRN_TIME_PACK(tv.tv_sec, |
| 89 | GRN_TIME_NSEC_TO_USEC(tv.tv_nsec))); |
| 90 | } |
| 91 | |
| 92 | static bool |
| 93 | grn_time_t_to_tm(grn_ctx *ctx, const grn_time_t time, struct tm *tm) |
| 94 | { |
| 95 | bool success; |
| 96 | const char *function_name; |
| 97 | #ifdef HAVE__LOCALTIME64_S |
| 98 | function_name = "localtime_s"; |
| 99 | success = (_localtime64_s(tm, &time) == 0); |
| 100 | #else /* HAVE__LOCALTIME64_S */ |
| 101 | # ifdef HAVE_LOCALTIME_R |
| 102 | function_name = "localtime_r"; |
| 103 | success = (localtime_r(&time, tm) != NULL); |
| 104 | # else /* HAVE_LOCALTIME_R */ |
| 105 | function_name = "localtime"; |
| 106 | { |
| 107 | struct tm *local_tm; |
| 108 | local_tm = localtime(&time); |
| 109 | if (local_tm) { |
| 110 | success = true; |
| 111 | memcpy(tm, local_tm, sizeof(struct tm)); |
| 112 | } else { |
| 113 | success = false; |
| 114 | } |
| 115 | } |
| 116 | # endif /* HAVE_LOCALTIME_R */ |
| 117 | #endif /* HAVE__LOCALTIME64_S */ |
| 118 | if (!success) { |
| 119 | SERR("%s: failed to convert time_t to struct tm: <%" GRN_FMT_INT64D ">", |
| 120 | function_name, |
| 121 | (int64_t)time); |
| 122 | } |
| 123 | return success; |
| 124 | } |
| 125 | |
| 126 | struct tm * |
| 127 | grn_timeval2tm(grn_ctx *ctx, grn_timeval *tv, struct tm *tm) |
| 128 | { |
| 129 | if (grn_time_t_to_tm(ctx, tv->tv_sec, tm)) { |
| 130 | return tm; |
| 131 | } else { |
| 132 | return NULL; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | bool |
| 137 | grn_time_to_tm(grn_ctx *ctx, int64_t time, struct tm *tm) |
| 138 | { |
| 139 | int64_t sec; |
| 140 | int32_t usec; |
| 141 | |
| 142 | GRN_TIME_UNPACK(time, sec, usec); |
| 143 | return grn_time_t_to_tm(ctx, sec, tm); |
| 144 | } |
| 145 | |
| 146 | static bool |
| 147 | grn_time_t_from_tm(grn_ctx *ctx, grn_time_t *time, struct tm *tm) |
| 148 | { |
| 149 | tm->tm_yday = -1; |
| 150 | *time = grn_mktime(tm); |
| 151 | /* We can't use (*time != -1) because -1 is a valid UNIX time |
| 152 | * (1969-12-31T23:59:59Z). */ |
| 153 | bool success = (tm->tm_yday != -1); |
| 154 | if (!success) { |
| 155 | ERR(GRN_INVALID_ARGUMENT, |
| 156 | "mktime: failed to convert struct tm to time_t: " |
| 157 | "<%04d-%02d-%02dT%02d:%02d:%02d>(%d)", |
| 158 | 1900 + tm->tm_year, |
| 159 | tm->tm_mon + 1, |
| 160 | tm->tm_mday, |
| 161 | tm->tm_hour, |
| 162 | tm->tm_min, |
| 163 | tm->tm_sec, |
| 164 | tm->tm_isdst); |
| 165 | } |
| 166 | return success; |
| 167 | } |
| 168 | |
| 169 | bool |
| 170 | grn_time_from_tm(grn_ctx *ctx, int64_t *time, struct tm *tm) |
| 171 | { |
| 172 | grn_time_t sec_time_t; |
| 173 | int64_t sec; |
| 174 | int32_t usec = 0; |
| 175 | |
| 176 | if (!grn_time_t_from_tm(ctx, &sec_time_t, tm)) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | sec = sec_time_t; |
| 181 | *time = GRN_TIME_PACK(sec, usec); |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | grn_rc |
| 186 | grn_timeval2str(grn_ctx *ctx, grn_timeval *tv, char *buf, size_t buf_size) |
| 187 | { |
| 188 | struct tm tm; |
| 189 | struct tm *ltm; |
| 190 | ltm = grn_timeval2tm(ctx, tv, &tm); |
| 191 | grn_snprintf(buf, buf_size, GRN_TIMEVAL_STR_SIZE, |
| 192 | GRN_TIMEVAL_STR_FORMAT, |
| 193 | ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, |
| 194 | ltm->tm_hour, ltm->tm_min, ltm->tm_sec, |
| 195 | (int)(GRN_TIME_NSEC_TO_USEC(tv->tv_nsec))); |
| 196 | if (buf_size > GRN_TIMEVAL_STR_SIZE) { |
| 197 | buf[GRN_TIMEVAL_STR_SIZE - 1] = '\0'; |
| 198 | } else { |
| 199 | buf[buf_size - 1] = '\0'; |
| 200 | } |
| 201 | return ctx->rc; |
| 202 | } |
| 203 | |
| 204 | static grn_rc |
| 205 | grn_str2timeval_offset_sec(const char *start, const char *end, bool *have_offset, int32_t *gm_offset_sec) |
| 206 | { |
| 207 | const char *position = start; |
| 208 | int32_t hours, minutes = 0, sign = 1; |
| 209 | |
| 210 | *have_offset = false; |
| 211 | *gm_offset_sec = 0; |
| 212 | |
| 213 | if (position >= end) { return GRN_SUCCESS; } |
| 214 | |
| 215 | switch (*position) { |
| 216 | case '+' : |
| 217 | sign = 1; |
| 218 | *have_offset = true; |
| 219 | break; |
| 220 | case '-' : |
| 221 | sign = -1; |
| 222 | *have_offset = true; |
| 223 | break; |
| 224 | case 'Z' : |
| 225 | case 'z' : |
| 226 | *have_offset = true; |
| 227 | return GRN_SUCCESS; |
| 228 | default : |
| 229 | return GRN_INVALID_ARGUMENT; |
| 230 | } |
| 231 | |
| 232 | position++; |
| 233 | if (position >= end) { return GRN_INVALID_ARGUMENT; } |
| 234 | |
| 235 | hours = grn_atoi(position, end, &position); |
| 236 | if (hours < 0 || hours >= 24) { return GRN_INVALID_ARGUMENT; } |
| 237 | |
| 238 | if (position < end && *position == ':') { |
| 239 | position++; |
| 240 | if (position == end) { return GRN_INVALID_ARGUMENT; } |
| 241 | |
| 242 | minutes = grn_atoi(position, end, &position); |
| 243 | if (minutes < 0 || minutes >= 60) { return GRN_INVALID_ARGUMENT; } |
| 244 | } |
| 245 | |
| 246 | *gm_offset_sec = sign * (hours * 3600 + minutes * 60); |
| 247 | |
| 248 | return GRN_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | grn_rc |
| 252 | grn_str2timeval(const char *str, uint32_t str_len, grn_timeval *tv) |
| 253 | { |
| 254 | struct tm tm; |
| 255 | const char *r1, *r2, *rend = str + str_len; |
| 256 | uint32_t uv = 0; |
| 257 | |
| 258 | memset(&tm, 0, sizeof(struct tm)); |
| 259 | |
| 260 | tm.tm_year = (int)grn_atoui(str, rend, &r1) - 1900; |
| 261 | if ((r1 + 1) >= rend || (*r1 != '/' && *r1 != '-')) { |
| 262 | return GRN_INVALID_ARGUMENT; |
| 263 | } |
| 264 | r1++; |
| 265 | tm.tm_mon = (int)grn_atoui(r1, rend, &r1) - 1; |
| 266 | if ((r1 + 1) >= rend || (*r1 != '/' && *r1 != '-') || |
| 267 | tm.tm_mon < 0 || tm.tm_mon >= 12) { return GRN_INVALID_ARGUMENT; } |
| 268 | r1++; |
| 269 | tm.tm_mday = (int)grn_atoui(r1, rend, &r1); |
| 270 | if (tm.tm_mday < 1 || tm.tm_mday > 31) { |
| 271 | return GRN_INVALID_ARGUMENT; |
| 272 | } |
| 273 | if (r1 == rend) { |
| 274 | /* YYYY-MM-DD */ |
| 275 | r2 = r1; |
| 276 | } else { |
| 277 | /* YYYY-MM-DD[ Tt]hh:mm:ss */ |
| 278 | if ((r1 + 1) >= rend || (*r1 != ' ' && *r1 != 'T' && *r1 != 't')) { |
| 279 | return GRN_INVALID_ARGUMENT; |
| 280 | } |
| 281 | |
| 282 | tm.tm_hour = (int)grn_atoui(++r1, rend, &r2); |
| 283 | if ((r2 + 1) >= rend || r1 == r2 || *r2 != ':' || |
| 284 | tm.tm_hour < 0 || tm.tm_hour >= 24) { |
| 285 | return GRN_INVALID_ARGUMENT; |
| 286 | } |
| 287 | r1 = r2 + 1; |
| 288 | tm.tm_min = (int)grn_atoui(r1, rend, &r2); |
| 289 | if ((r2 + 1) >= rend || r1 == r2 || *r2 != ':' || |
| 290 | tm.tm_min < 0 || tm.tm_min >= 60) { |
| 291 | return GRN_INVALID_ARGUMENT; |
| 292 | } |
| 293 | r1 = r2 + 1; |
| 294 | tm.tm_sec = (int)grn_atoui(r1, rend, &r2); |
| 295 | if (r1 == r2 || |
| 296 | tm.tm_sec < 0 || tm.tm_sec > 61 /* leap 2sec */) { |
| 297 | return GRN_INVALID_ARGUMENT; |
| 298 | } |
| 299 | r1 = r2; |
| 300 | |
| 301 | if ((r1 + 1) < rend && *r1 == '.') { |
| 302 | /* YYYY-MM-DD[ Tt]hh:mm:ss.UUUUUU */ |
| 303 | uv = grn_atoui(++r1, rend, &r2); |
| 304 | |
| 305 | for (int i = 0; r2 + i < r1 + 6; i++) { |
| 306 | uv *= 10; |
| 307 | } |
| 308 | if (uv >= GRN_TIME_USEC_PER_SEC) { return GRN_INVALID_ARGUMENT; } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | tm.tm_yday = -1; |
| 313 | tm.tm_isdst = -1; |
| 314 | |
| 315 | { |
| 316 | bool have_offset = false; |
| 317 | int32_t gm_offset_sec = 0; |
| 318 | grn_rc rc = GRN_SUCCESS; |
| 319 | |
| 320 | rc = grn_str2timeval_offset_sec(r2, rend, &have_offset, &gm_offset_sec); |
| 321 | |
| 322 | if (rc != GRN_SUCCESS) { return rc; } |
| 323 | |
| 324 | if (have_offset) { |
| 325 | tv->tv_sec = grn_timegm(&tm); |
| 326 | tv->tv_sec -= gm_offset_sec; |
| 327 | } else { |
| 328 | tv->tv_sec = grn_mktime(&tm); |
| 329 | } |
| 330 | /* tm_yday is set appropriately (0-365) on successful completion. */ |
| 331 | if (tm.tm_yday == -1) { return GRN_INVALID_ARGUMENT; } |
| 332 | } |
| 333 | |
| 334 | tv->tv_nsec = (int32_t)GRN_TIME_USEC_TO_NSEC(uv); |
| 335 | return GRN_SUCCESS; |
| 336 | } |