osTime.c 16.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
Shengliang Guan 已提交
17
#define _BSD_SOURCE
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#ifdef DARWIN
#define _XOPEN_SOURCE
S
Shengliang Guan 已提交
21
#else
S
Shengliang Guan 已提交
22
#define _XOPEN_SOURCE 500
S
Shengliang Guan 已提交
23 24
#endif

S
Shengliang Guan 已提交
25 26 27 28
#define _DEFAULT_SOURCE

#include "os.h"

wafwerar's avatar
wafwerar 已提交
29
#ifdef WINDOWS
S
Shengliang Guan 已提交
30

wafwerar's avatar
wafwerar 已提交
31
#include <stdlib.h>
wafwerar's avatar
wafwerar 已提交
32
#include <string.h>
wafwerar's avatar
wafwerar 已提交
33
#include <time.h>
wafwerar's avatar
wafwerar 已提交
34
//#define TM_YEAR_BASE 1970 //origin
wafwerar's avatar
wafwerar 已提交
35
#define TM_YEAR_BASE 1900  // slguan
36 37 38 39

// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const uint64_t TIMEEPOCH = ((uint64_t)116444736000000000ULL);
40 41 42
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1900
static const uint64_t TIMEEPOCH1900 = ((uint64_t)116445024000000000ULL);
43

wafwerar's avatar
wafwerar 已提交
44
/*
wafwerar's avatar
wafwerar 已提交
45 46 47 48 49 50 51 52 53
 * We do not implement alternate representations. However, we always
 * check whether a given modifier is allowed for a certain conversion.
 */
#define ALT_E 0x01
#define ALT_O 0x02
#define LEGAL_ALT(x)                   \
  {                                    \
    if (alt_format & ~(x)) return (0); \
  }
wafwerar's avatar
wafwerar 已提交
54

wafwerar's avatar
wafwerar 已提交
55 56
static int conv_num(const char **buf, int *dest, int llim, int ulim) {
  int result = 0;
wafwerar's avatar
wafwerar 已提交
57

wafwerar's avatar
wafwerar 已提交
58 59
  /* The limit also determines the number of valid digits. */
  int rulim = ulim;
wafwerar's avatar
wafwerar 已提交
60

wafwerar's avatar
wafwerar 已提交
61
  if (**buf < '0' || **buf > '9') return (0);
wafwerar's avatar
wafwerar 已提交
62

wafwerar's avatar
wafwerar 已提交
63 64 65 66 67
  do {
    result *= 10;
    result += *(*buf)++ - '0';
    rulim /= 10;
  } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
wafwerar's avatar
wafwerar 已提交
68

wafwerar's avatar
wafwerar 已提交
69
  if (result < llim || result > ulim) return (0);
wafwerar's avatar
wafwerar 已提交
70

wafwerar's avatar
wafwerar 已提交
71 72
  *dest = result;
  return (1);
wafwerar's avatar
wafwerar 已提交
73 74
}

wafwerar's avatar
wafwerar 已提交
75 76 77 78 79 80
static const char *day[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static const char *abday[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static const char *mon[12] = {"January", "February", "March",     "April",   "May",      "June",
                              "July",    "August",   "September", "October", "November", "December"};
static const char *abmon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static const char *am_pm[2] = {"AM", "PM"};
wafwerar's avatar
wafwerar 已提交
81 82 83 84 85 86

#else
#include <sys/time.h>
#endif

char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm) {
wafwerar's avatar
wafwerar 已提交
87
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
88 89 90 91
  char        c;
  const char *bp;
  size_t      len = 0;
  int         alt_format, i, split_year = 0;
wafwerar's avatar
wafwerar 已提交
92

wafwerar's avatar
wafwerar 已提交
93
  bp = buf;
wafwerar's avatar
wafwerar 已提交
94

wafwerar's avatar
wafwerar 已提交
95 96 97
  while ((c = *fmt) != '\0') {
    /* Clear `alternate' modifier prior to new conversion. */
    alt_format = 0;
wafwerar's avatar
wafwerar 已提交
98

wafwerar's avatar
wafwerar 已提交
99 100 101
    /* Eat up white-space. */
    if (isspace(c)) {
      while (isspace(*bp)) bp++;
wafwerar's avatar
wafwerar 已提交
102

wafwerar's avatar
wafwerar 已提交
103 104 105
      fmt++;
      continue;
    }
wafwerar's avatar
wafwerar 已提交
106

wafwerar's avatar
wafwerar 已提交
107
    if ((c = *fmt++) != '%') goto literal;
wafwerar's avatar
wafwerar 已提交
108

wafwerar's avatar
wafwerar 已提交
109 110 111 112 113
  again:
    switch (c = *fmt++) {
      case '%': /* "%%" is converted to "%". */
      literal:
        if (c != *bp++) return (0);
wafwerar's avatar
wafwerar 已提交
114 115 116
        break;

        /*
wafwerar's avatar
wafwerar 已提交
117 118 119 120
         * "Alternative" modifiers. Just set the appropriate flag
         * and start over again.
         */
      case 'E': /* "%E?" alternative conversion modifier. */
wafwerar's avatar
wafwerar 已提交
121 122 123 124
        LEGAL_ALT(0);
        alt_format |= ALT_E;
        goto again;

wafwerar's avatar
wafwerar 已提交
125
      case 'O': /* "%O?" alternative conversion modifier. */
wafwerar's avatar
wafwerar 已提交
126 127 128 129 130
        LEGAL_ALT(0);
        alt_format |= ALT_O;
        goto again;

        /*
wafwerar's avatar
wafwerar 已提交
131 132 133
         * "Complex" conversion rules, implemented through recursion.
         */
      case 'c': /* Date and time, using the locale's format. */
wafwerar's avatar
wafwerar 已提交
134
        LEGAL_ALT(ALT_E);
wafwerar's avatar
wafwerar 已提交
135
        if (!(bp = taosStrpTime(bp, "%x %X", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
136 137
        break;

wafwerar's avatar
wafwerar 已提交
138
      case 'D': /* The date as "%m/%d/%y". */
wafwerar's avatar
wafwerar 已提交
139
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
140
        if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
141 142
        break;

wafwerar's avatar
wafwerar 已提交
143
      case 'R': /* The time as "%H:%M". */
wafwerar's avatar
wafwerar 已提交
144
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
145
        if (!(bp = taosStrpTime(bp, "%H:%M", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
146 147
        break;

wafwerar's avatar
wafwerar 已提交
148
      case 'r': /* The time in 12-hour clock representation. */
wafwerar's avatar
wafwerar 已提交
149
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
150
        if (!(bp = taosStrpTime(bp, "%I:%M:%S %p", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
151 152
        break;

wafwerar's avatar
wafwerar 已提交
153
      case 'T': /* The time as "%H:%M:%S". */
wafwerar's avatar
wafwerar 已提交
154
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
155
        if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
156 157
        break;

wafwerar's avatar
wafwerar 已提交
158
      case 'X': /* The time, using the locale's format. */
wafwerar's avatar
wafwerar 已提交
159
        LEGAL_ALT(ALT_E);
wafwerar's avatar
wafwerar 已提交
160
        if (!(bp = taosStrpTime(bp, "%H:%M:%S", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
161 162
        break;

wafwerar's avatar
wafwerar 已提交
163
      case 'x': /* The date, using the locale's format. */
wafwerar's avatar
wafwerar 已提交
164
        LEGAL_ALT(ALT_E);
wafwerar's avatar
wafwerar 已提交
165
        if (!(bp = taosStrpTime(bp, "%m/%d/%y", tm))) return (0);
wafwerar's avatar
wafwerar 已提交
166 167 168
        break;

        /*
wafwerar's avatar
wafwerar 已提交
169 170 171 172
         * "Elementary" conversion rules.
         */
      case 'A': /* The day of week, using the locale's form. */
      case 'a':
wafwerar's avatar
wafwerar 已提交
173 174
        LEGAL_ALT(0);
        for (i = 0; i < 7; i++) {
wafwerar's avatar
wafwerar 已提交
175 176 177 178 179 180 181
          /* Full name. */
          len = strlen(day[i]);
          if (strncmp(day[i], bp, len) == 0) break;

          /* Abbreviated name. */
          len = strlen(abday[i]);
          if (strncmp(abday[i], bp, len) == 0) break;
wafwerar's avatar
wafwerar 已提交
182 183 184
        }

        /* Nothing matched. */
wafwerar's avatar
wafwerar 已提交
185
        if (i == 7) return (0);
wafwerar's avatar
wafwerar 已提交
186 187 188 189 190

        tm->tm_wday = i;
        bp += len;
        break;

wafwerar's avatar
wafwerar 已提交
191 192 193
      case 'B': /* The month, using the locale's form. */
      case 'b':
      case 'h':
wafwerar's avatar
wafwerar 已提交
194 195
        LEGAL_ALT(0);
        for (i = 0; i < 12; i++) {
wafwerar's avatar
wafwerar 已提交
196 197 198 199 200 201 202
          /* Full name. */
          len = strlen(mon[i]);
          if (strncmp(mon[i], bp, len) == 0) break;

          /* Abbreviated name. */
          len = strlen(abmon[i]);
          if (strncmp(abmon[i], bp, len) == 0) break;
wafwerar's avatar
wafwerar 已提交
203 204 205
        }

        /* Nothing matched. */
wafwerar's avatar
wafwerar 已提交
206
        if (i == 12) return (0);
wafwerar's avatar
wafwerar 已提交
207 208 209 210 211

        tm->tm_mon = i;
        bp += len;
        break;

wafwerar's avatar
wafwerar 已提交
212
      case 'C': /* The century number. */
wafwerar's avatar
wafwerar 已提交
213
        LEGAL_ALT(ALT_E);
wafwerar's avatar
wafwerar 已提交
214
        if (!(conv_num(&bp, &i, 0, 99))) return (0);
wafwerar's avatar
wafwerar 已提交
215 216

        if (split_year) {
wafwerar's avatar
wafwerar 已提交
217 218 219 220
          tm->tm_year = (tm->tm_year % 100) + (i * 100);
        } else {
          tm->tm_year = i * 100;
          split_year = 1;
wafwerar's avatar
wafwerar 已提交
221 222 223
        }
        break;

wafwerar's avatar
wafwerar 已提交
224 225
      case 'd': /* The day of month. */
      case 'e':
wafwerar's avatar
wafwerar 已提交
226
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
227
        if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) return (0);
wafwerar's avatar
wafwerar 已提交
228
        break;
S
Shengliang Guan 已提交
229

wafwerar's avatar
wafwerar 已提交
230
      case 'k': /* The hour (24-hour clock representation). */
wafwerar's avatar
wafwerar 已提交
231 232
        LEGAL_ALT(0);
        /* FALLTHROUGH */
wafwerar's avatar
wafwerar 已提交
233
      case 'H':
wafwerar's avatar
wafwerar 已提交
234
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
235
        if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) return (0);
wafwerar's avatar
wafwerar 已提交
236 237
        break;

wafwerar's avatar
wafwerar 已提交
238
      case 'l': /* The hour (12-hour clock representation). */
wafwerar's avatar
wafwerar 已提交
239 240
        LEGAL_ALT(0);
        /* FALLTHROUGH */
wafwerar's avatar
wafwerar 已提交
241
      case 'I':
wafwerar's avatar
wafwerar 已提交
242
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
243 244
        if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) return (0);
        if (tm->tm_hour == 12) tm->tm_hour = 0;
wafwerar's avatar
wafwerar 已提交
245 246
        break;

wafwerar's avatar
wafwerar 已提交
247
      case 'j': /* The day of year. */
wafwerar's avatar
wafwerar 已提交
248
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
249
        if (!(conv_num(&bp, &i, 1, 366))) return (0);
wafwerar's avatar
wafwerar 已提交
250 251 252
        tm->tm_yday = i - 1;
        break;

wafwerar's avatar
wafwerar 已提交
253
      case 'M': /* The minute. */
wafwerar's avatar
wafwerar 已提交
254
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
255
        if (!(conv_num(&bp, &tm->tm_min, 0, 59))) return (0);
wafwerar's avatar
wafwerar 已提交
256 257
        break;

wafwerar's avatar
wafwerar 已提交
258
      case 'm': /* The month. */
wafwerar's avatar
wafwerar 已提交
259
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
260
        if (!(conv_num(&bp, &i, 1, 12))) return (0);
wafwerar's avatar
wafwerar 已提交
261 262 263
        tm->tm_mon = i - 1;
        break;

wafwerar's avatar
wafwerar 已提交
264
      case 'p': /* The locale's equivalent of AM/PM. */
wafwerar's avatar
wafwerar 已提交
265 266 267
        LEGAL_ALT(0);
        /* AM? */
        if (strcmp(am_pm[0], bp) == 0) {
wafwerar's avatar
wafwerar 已提交
268
          if (tm->tm_hour > 11) return (0);
wafwerar's avatar
wafwerar 已提交
269

wafwerar's avatar
wafwerar 已提交
270 271
          bp += strlen(am_pm[0]);
          break;
wafwerar's avatar
wafwerar 已提交
272 273 274
        }
        /* PM? */
        else if (strcmp(am_pm[1], bp) == 0) {
wafwerar's avatar
wafwerar 已提交
275
          if (tm->tm_hour > 11) return (0);
wafwerar's avatar
wafwerar 已提交
276

wafwerar's avatar
wafwerar 已提交
277 278 279
          tm->tm_hour += 12;
          bp += strlen(am_pm[1]);
          break;
wafwerar's avatar
wafwerar 已提交
280 281 282 283 284
        }

        /* Nothing matched. */
        return (0);

wafwerar's avatar
wafwerar 已提交
285
      case 'S': /* The seconds. */
wafwerar's avatar
wafwerar 已提交
286
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
287
        if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) return (0);
wafwerar's avatar
wafwerar 已提交
288 289
        break;

wafwerar's avatar
wafwerar 已提交
290 291
      case 'U': /* The week of year, beginning on sunday. */
      case 'W': /* The week of year, beginning on monday. */
wafwerar's avatar
wafwerar 已提交
292 293
        LEGAL_ALT(ALT_O);
        /*
wafwerar's avatar
wafwerar 已提交
294 295 296 297 298 299
         * XXX This is bogus, as we can not assume any valid
         * information present in the tm structure at this
         * point to calculate a real value, so just check the
         * range for now.
         */
        if (!(conv_num(&bp, &i, 0, 53))) return (0);
wafwerar's avatar
wafwerar 已提交
300 301
        break;

wafwerar's avatar
wafwerar 已提交
302
      case 'w': /* The day of week, beginning on sunday. */
wafwerar's avatar
wafwerar 已提交
303
        LEGAL_ALT(ALT_O);
wafwerar's avatar
wafwerar 已提交
304
        if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) return (0);
wafwerar's avatar
wafwerar 已提交
305 306
        break;

wafwerar's avatar
wafwerar 已提交
307
      case 'Y': /* The year. */
wafwerar's avatar
wafwerar 已提交
308
        LEGAL_ALT(ALT_E);
wafwerar's avatar
wafwerar 已提交
309
        if (!(conv_num(&bp, &i, 0, 9999))) return (0);
wafwerar's avatar
wafwerar 已提交
310 311 312 313

        tm->tm_year = i - TM_YEAR_BASE;
        break;

wafwerar's avatar
wafwerar 已提交
314
      case 'y': /* The year within 100 years of the epoch. */
wafwerar's avatar
wafwerar 已提交
315
        LEGAL_ALT(ALT_E | ALT_O);
wafwerar's avatar
wafwerar 已提交
316
        if (!(conv_num(&bp, &i, 0, 99))) return (0);
wafwerar's avatar
wafwerar 已提交
317 318

        if (split_year) {
wafwerar's avatar
wafwerar 已提交
319 320
          tm->tm_year = ((tm->tm_year / 100) * 100) + i;
          break;
wafwerar's avatar
wafwerar 已提交
321 322 323
        }
        split_year = 1;
        if (i <= 68)
wafwerar's avatar
wafwerar 已提交
324
          tm->tm_year = i + 2000 - TM_YEAR_BASE;
wafwerar's avatar
wafwerar 已提交
325
        else
wafwerar's avatar
wafwerar 已提交
326
          tm->tm_year = i + 1900 - TM_YEAR_BASE;
wafwerar's avatar
wafwerar 已提交
327 328 329
        break;

        /*
wafwerar's avatar
wafwerar 已提交
330 331 332 333
         * Miscellaneous conversions.
         */
      case 'n': /* Any kind of white-space. */
      case 't':
wafwerar's avatar
wafwerar 已提交
334
        LEGAL_ALT(0);
wafwerar's avatar
wafwerar 已提交
335
        while (isspace(*bp)) bp++;
wafwerar's avatar
wafwerar 已提交
336 337
        break;

wafwerar's avatar
wafwerar 已提交
338
      default: /* Unknown/unsupported conversion. */
wafwerar's avatar
wafwerar 已提交
339 340
        return (0);
    }
wafwerar's avatar
wafwerar 已提交
341
  }
wafwerar's avatar
wafwerar 已提交
342

wafwerar's avatar
wafwerar 已提交
343 344
  /* LINTED functional specification */
  return ((char *)bp);
wafwerar's avatar
wafwerar 已提交
345
#else
wafwerar's avatar
wafwerar 已提交
346
  return strptime(buf, fmt, tm);
wafwerar's avatar
wafwerar 已提交
347 348 349
#endif
}

H
Haojun Liao 已提交
350
int32_t taosGetTimeOfDay(struct timeval *tv) {
wafwerar's avatar
wafwerar 已提交
351
#ifdef WINDOWS
352 353
  LARGE_INTEGER t;
  FILETIME      f;
S
Shengliang Guan 已提交
354

355 356 357 358
  GetSystemTimeAsFileTime(&f);
  t.QuadPart = f.dwHighDateTime;
  t.QuadPart <<= 32;
  t.QuadPart |= f.dwLowDateTime;
S
Shengliang Guan 已提交
359

360 361 362
  t.QuadPart -= TIMEEPOCH;
  tv->tv_sec = t.QuadPart / 10000000;
  tv->tv_usec = (t.QuadPart % 10000000) / 10;
wafwerar's avatar
wafwerar 已提交
363 364 365
#else
  return gettimeofday(tv, NULL);
#endif
S
Shengliang Guan 已提交
366 367
}

wafwerar's avatar
wafwerar 已提交
368
time_t taosTime(time_t *t) { return time(t); }
369

370 371
time_t taosMktime(struct tm *timep) {
#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
372 373 374 375 376 377 378 379 380
  struct tm     tm1 = {0};
  LARGE_INTEGER t;
  FILETIME      f;
  SYSTEMTIME    s;
  FILETIME      ff;
  SYSTEMTIME    ss;
  LARGE_INTEGER offset;

  time_t tt = 0;
381 382 383
  localtime_s(&tm1, &tt);
  ss.wYear = tm1.tm_year + 1900;
  ss.wMonth = tm1.tm_mon + 1;
wafwerar's avatar
wafwerar 已提交
384
  ss.wDay = tm1.tm_mday;
385 386 387 388 389 390 391 392 393 394 395
  ss.wHour = tm1.tm_hour;
  ss.wMinute = tm1.tm_min;
  ss.wSecond = tm1.tm_sec;
  ss.wMilliseconds = 0;
  SystemTimeToFileTime(&ss, &ff);
  offset.QuadPart = ff.dwHighDateTime;
  offset.QuadPart <<= 32;
  offset.QuadPart |= ff.dwLowDateTime;

  s.wYear = timep->tm_year + 1900;
  s.wMonth = timep->tm_mon + 1;
wafwerar's avatar
wafwerar 已提交
396
  s.wDay = timep->tm_mday;
397 398 399 400 401 402 403 404 405 406
  s.wHour = timep->tm_hour;
  s.wMinute = timep->tm_min;
  s.wSecond = timep->tm_sec;
  s.wMilliseconds = 0;
  SystemTimeToFileTime(&s, &f);
  t.QuadPart = f.dwHighDateTime;
  t.QuadPart <<= 32;
  t.QuadPart |= f.dwLowDateTime;

  t.QuadPart -= offset.QuadPart;
dengyihao's avatar
dengyihao 已提交
407
  return (time_t)(t.QuadPart / 10000000);
408 409 410
#else
  return mktime(timep);
#endif
dengyihao's avatar
dengyihao 已提交
411
}
412

413 414
struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
  struct tm *res = NULL;
415 416 417
  if (timep == NULL) {
    return NULL;
  }
418
  if (result == NULL) {
419 420 421 422 423
    res = localtime(timep);
    if (res == NULL && buf != NULL) {
      sprintf(buf, "NaN");
    }
    return res;
424
  }
wafwerar's avatar
wafwerar 已提交
425
#ifdef WINDOWS
426
  if (*timep < 0) {
427 428 429 430 431
    if (*timep < -2208988800LL) {
      if (buf != NULL) {
        sprintf(buf, "NaN");
      }
      return NULL;
432
    }
433 434 435

    SYSTEMTIME    s;
    FILETIME      f;
436 437 438
    LARGE_INTEGER offset;
    struct tm     tm1;
    time_t        tt = 0;
G
fix  
Ganlin Zhao 已提交
439
    if (localtime_s(&tm1, &tt) != 0 ) {
440 441 442
      if (buf != NULL) {
        sprintf(buf, "NaN");
      }
G
fix  
Ganlin Zhao 已提交
443 444
      return NULL;
    }
445
    offset.QuadPart = TIMEEPOCH1900;
446 447 448 449 450 451 452 453
    offset.QuadPart += *timep * 10000000;
    f.dwLowDateTime = offset.QuadPart & 0xffffffff;
    f.dwHighDateTime = (offset.QuadPart >> 32) & 0xffffffff;
    FileTimeToSystemTime(&f, &s);
    result->tm_sec = s.wSecond;
    result->tm_min = s.wMinute;
    result->tm_hour = s.wHour;
    result->tm_mday = s.wDay;
dengyihao's avatar
dengyihao 已提交
454 455
    result->tm_mon = s.wMonth - 1;
    result->tm_year = s.wYear - 1900;
456 457 458 459
    result->tm_wday = s.wDayOfWeek;
    result->tm_yday = 0;
    result->tm_isdst = 0;
  } else {
G
fix  
Ganlin Zhao 已提交
460
    if (localtime_s(result, timep) != 0) {
461 462 463
      if (buf != NULL) {
        sprintf(buf, "NaN");
      }
464 465
      return NULL;
    }
466
  }
S
Shengliang Guan 已提交
467
#else
468 469 470 471
  res = localtime_r(timep, result);
  if (res == NULL && buf != NULL) {
    sprintf(buf, "NaN");
  }
wafwerar's avatar
wafwerar 已提交
472 473
#endif
  return result;
S
Shengliang Guan 已提交
474 475
}

dengyihao's avatar
dengyihao 已提交
476 477 478 479 480 481 482 483 484 485
static int isLeapYear(time_t year) {
  if (year % 4)
    return 0;
  else if (year % 100)
    return 1;
  else if (year % 400)
    return 0;
  else
    return 1;
}
H
Haojun Liao 已提交
486

dengyihao's avatar
dengyihao 已提交
487
struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) {
dengyihao's avatar
dengyihao 已提交
488 489 490 491 492
  if (result == NULL) {
    return localtime(timep);
  }
#ifdef WINDOWS
  if (*timep < 0) {
G
fix  
Ganlin Zhao 已提交
493 494
    return NULL;
    // TODO: bugs in following code
dengyihao's avatar
dengyihao 已提交
495 496 497 498 499
    SYSTEMTIME    ss, s;
    FILETIME      ff, f;
    LARGE_INTEGER offset;
    struct tm     tm1;
    time_t        tt = 0;
G
fix  
Ganlin Zhao 已提交
500 501 502
    if (localtime_s(&tm1, &tt) != 0) {
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    ss.wYear = tm1.tm_year + 1900;
    ss.wMonth = tm1.tm_mon + 1;
    ss.wDay = tm1.tm_mday;
    ss.wHour = tm1.tm_hour;
    ss.wMinute = tm1.tm_min;
    ss.wSecond = tm1.tm_sec;
    ss.wMilliseconds = 0;
    SystemTimeToFileTime(&ss, &ff);
    offset.QuadPart = ff.dwHighDateTime;
    offset.QuadPart <<= 32;
    offset.QuadPart |= ff.dwLowDateTime;
    offset.QuadPart += *timep * 10000000;
    f.dwLowDateTime = offset.QuadPart & 0xffffffff;
    f.dwHighDateTime = (offset.QuadPart >> 32) & 0xffffffff;
    FileTimeToSystemTime(&f, &s);
    result->tm_sec = s.wSecond;
    result->tm_min = s.wMinute;
    result->tm_hour = s.wHour;
    result->tm_mday = s.wDay;
    result->tm_mon = s.wMonth - 1;
    result->tm_year = s.wYear - 1900;
    result->tm_wday = s.wDayOfWeek;
    result->tm_yday = 0;
    result->tm_isdst = 0;
  } else {
G
fix  
Ganlin Zhao 已提交
528
    if (localtime_s(result, timep) != 0) {
529 530
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
531
  }
dengyihao's avatar
dengyihao 已提交
532
#elif defined(LINUX)
dengyihao's avatar
dengyihao 已提交
533
  time_t secsMin = 60, secsHour = 3600, secsDay = 3600 * 24;
dengyihao's avatar
dengyihao 已提交
534
  long   tz = timezone;
dengyihao's avatar
dengyihao 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

  time_t t = *timep;
  t -= tz;                      /* Adjust for timezone. */
  t += 3600 * dst;              /* Adjust for daylight time. */
  time_t days = t / secsDay;    /* Days passed since epoch. */
  time_t seconds = t % secsDay; /* Remaining seconds. */

  result->tm_isdst = dst;
  result->tm_hour = seconds / secsHour;
  result->tm_min = (seconds % secsHour) / secsMin;
  result->tm_sec = (seconds % secsHour) % secsMin;

  /* 1/1/1970 was a Thursday, that is, day 4 from the POV of the tm structure
   * where sunday = 0, so to calculate the day of the week we have to add 4
   * and take the modulo by 7. */
  result->tm_wday = (days + 4) % 7;

  /* Calculate the current year. */
  result->tm_year = 1970;
  while (1) {
    /* Leap years have one day more. */
    time_t daysOfYear = 365 + isLeapYear(result->tm_year);
    if (daysOfYear > days) break;
    days -= daysOfYear;
    result->tm_year++;
  }
  result->tm_yday = days; /* Number of day of the current year. */
  /* We need to calculate in which month and day of the month we are. To do
   * so we need to skip days according to how many days there are in each
   * month, and adjust for the leap year that has one more day in February. */
  int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  mdays[1] += isLeapYear(result->tm_year);
  result->tm_mon = 0;
  while (days >= mdays[result->tm_mon]) {
    days -= mdays[result->tm_mon];
    result->tm_mon++;
  }

  result->tm_mday = days + 1; /* Add 1 since our 'days' is zero-based. */
  result->tm_year -= 1900;    /* Surprisingly tm_year is year-1900. */
dengyihao's avatar
dengyihao 已提交
575 576
#else
  localtime_r(timep, result);
dengyihao's avatar
dengyihao 已提交
577 578 579
#endif
  return result;
}
H
Haojun Liao 已提交
580

L
Liu Jicong 已提交
581
int32_t taosGetTimestampSec() { return (int32_t)time(NULL); }
H
Haojun Liao 已提交
582

wafwerar's avatar
wafwerar 已提交
583 584 585 586 587 588 589 590 591 592
int32_t taosClockGetTime(int clock_id, struct timespec *pTS) {
#ifdef WINDOWS
  LARGE_INTEGER        t;
  FILETIME             f;

  GetSystemTimeAsFileTime(&f);
  t.QuadPart = f.dwHighDateTime;
  t.QuadPart <<= 32;
  t.QuadPart |= f.dwLowDateTime;

593
  t.QuadPart -= TIMEEPOCH;
wafwerar's avatar
wafwerar 已提交
594
  pTS->tv_sec = t.QuadPart / 10000000;
dengyihao's avatar
dengyihao 已提交
595
  pTS->tv_nsec = (t.QuadPart % 10000000) * 100;
wafwerar's avatar
wafwerar 已提交
596 597 598 599
  return (0);
#else
  return clock_gettime(clock_id, pTS);
#endif
dengyihao's avatar
dengyihao 已提交
600
}