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 40

// 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);

wafwerar's avatar
wafwerar 已提交
41
/*
wafwerar's avatar
wafwerar 已提交
42 43 44 45 46 47 48 49 50
 * 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 已提交
51

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

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

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

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

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

wafwerar's avatar
wafwerar 已提交
68 69
  *dest = result;
  return (1);
wafwerar's avatar
wafwerar 已提交
70 71
}

wafwerar's avatar
wafwerar 已提交
72 73 74 75 76 77
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 已提交
78 79 80 81 82 83

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

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

wafwerar's avatar
wafwerar 已提交
90
  bp = buf;
wafwerar's avatar
wafwerar 已提交
91

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

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

wafwerar's avatar
wafwerar 已提交
100 101 102
      fmt++;
      continue;
    }
wafwerar's avatar
wafwerar 已提交
103

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

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

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

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

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

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

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

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

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

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

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

        /*
wafwerar's avatar
wafwerar 已提交
166 167 168 169
         * "Elementary" conversion rules.
         */
      case 'A': /* The day of week, using the locale's form. */
      case 'a':
wafwerar's avatar
wafwerar 已提交
170 171
        LEGAL_ALT(0);
        for (i = 0; i < 7; i++) {
wafwerar's avatar
wafwerar 已提交
172 173 174 175 176 177 178
          /* 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 已提交
179 180 181
        }

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

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

wafwerar's avatar
wafwerar 已提交
188 189 190
      case 'B': /* The month, using the locale's form. */
      case 'b':
      case 'h':
wafwerar's avatar
wafwerar 已提交
191 192
        LEGAL_ALT(0);
        for (i = 0; i < 12; i++) {
wafwerar's avatar
wafwerar 已提交
193 194 195 196 197 198 199
          /* 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 已提交
200 201 202
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

wafwerar's avatar
wafwerar 已提交
287 288
      case 'U': /* The week of year, beginning on sunday. */
      case 'W': /* The week of year, beginning on monday. */
wafwerar's avatar
wafwerar 已提交
289 290
        LEGAL_ALT(ALT_O);
        /*
wafwerar's avatar
wafwerar 已提交
291 292 293 294 295 296
         * 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 已提交
297 298
        break;

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

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

        tm->tm_year = i - TM_YEAR_BASE;
        break;

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

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

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

wafwerar's avatar
wafwerar 已提交
335
      default: /* Unknown/unsupported conversion. */
wafwerar's avatar
wafwerar 已提交
336 337
        return (0);
    }
wafwerar's avatar
wafwerar 已提交
338
  }
wafwerar's avatar
wafwerar 已提交
339

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

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

352 353 354 355
  GetSystemTimeAsFileTime(&f);
  t.QuadPart = f.dwHighDateTime;
  t.QuadPart <<= 32;
  t.QuadPart |= f.dwLowDateTime;
S
Shengliang Guan 已提交
356

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

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

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

  time_t tt = 0;
378 379 380
  localtime_s(&tm1, &tt);
  ss.wYear = tm1.tm_year + 1900;
  ss.wMonth = tm1.tm_mon + 1;
wafwerar's avatar
wafwerar 已提交
381
  ss.wDay = tm1.tm_mday;
382 383 384 385 386 387 388 389 390 391 392
  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 已提交
393
  s.wDay = timep->tm_mday;
394 395 396 397 398 399 400 401 402 403
  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 已提交
404
  return (time_t)(t.QuadPart / 10000000);
405 406 407
#else
  return mktime(timep);
#endif
dengyihao's avatar
dengyihao 已提交
408
}
409

410 411 412
struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
  struct tm *res = NULL;

413
  if (result == NULL) {
414 415 416 417 418
    res = localtime(timep);
    if (res == NULL && buf != NULL) {
      sprintf(buf, "NaN");
    }
    return res;
419
  }
wafwerar's avatar
wafwerar 已提交
420
#ifdef WINDOWS
421
  if (*timep < 0) {
422 423 424
    if (buf != NULL) {
      sprintf(buf, "NaN");
    }
G
fix  
Ganlin Zhao 已提交
425 426
    return NULL;
    // TODO: bugs in following code
dengyihao's avatar
dengyihao 已提交
427 428
    SYSTEMTIME    ss, s;
    FILETIME      ff, f;
429 430 431
    LARGE_INTEGER offset;
    struct tm     tm1;
    time_t        tt = 0;
G
fix  
Ganlin Zhao 已提交
432
    if (localtime_s(&tm1, &tt) != 0 ) {
433 434 435
      if (buf != NULL) {
        sprintf(buf, "NaN");
      }
G
fix  
Ganlin Zhao 已提交
436 437
      return NULL;
    }
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    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;
dengyihao's avatar
dengyihao 已提交
457 458
    result->tm_mon = s.wMonth - 1;
    result->tm_year = s.wYear - 1900;
459 460 461 462
    result->tm_wday = s.wDayOfWeek;
    result->tm_yday = 0;
    result->tm_isdst = 0;
  } else {
G
fix  
Ganlin Zhao 已提交
463
    if (localtime_s(result, timep) != 0) {
464 465 466
      if (buf != NULL) {
        sprintf(buf, "NaN");
      }
467 468
      return NULL;
    }
469
  }
S
Shengliang Guan 已提交
470
#else
471 472 473 474
  res = localtime_r(timep, result);
  if (res == NULL && buf != NULL) {
    sprintf(buf, "NaN");
  }
wafwerar's avatar
wafwerar 已提交
475 476
#endif
  return result;
S
Shengliang Guan 已提交
477 478
}

dengyihao's avatar
dengyihao 已提交
479 480 481 482 483 484 485 486 487 488
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 已提交
489

dengyihao's avatar
dengyihao 已提交
490
struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) {
dengyihao's avatar
dengyihao 已提交
491 492 493 494 495
  if (result == NULL) {
    return localtime(timep);
  }
#ifdef WINDOWS
  if (*timep < 0) {
G
fix  
Ganlin Zhao 已提交
496 497
    return NULL;
    // TODO: bugs in following code
dengyihao's avatar
dengyihao 已提交
498 499 500 501 502
    SYSTEMTIME    ss, s;
    FILETIME      ff, f;
    LARGE_INTEGER offset;
    struct tm     tm1;
    time_t        tt = 0;
G
fix  
Ganlin Zhao 已提交
503 504 505
    if (localtime_s(&tm1, &tt) != 0) {
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    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 已提交
531
    if (localtime_s(result, timep) != 0) {
532 533
      return NULL;
    }
dengyihao's avatar
dengyihao 已提交
534
  }
dengyihao's avatar
dengyihao 已提交
535
#elif defined(LINUX)
dengyihao's avatar
dengyihao 已提交
536
  time_t secsMin = 60, secsHour = 3600, secsDay = 3600 * 24;
dengyihao's avatar
dengyihao 已提交
537
  long   tz = timezone;
dengyihao's avatar
dengyihao 已提交
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 575 576 577

  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 已提交
578 579
#else
  localtime_r(timep, result);
dengyihao's avatar
dengyihao 已提交
580 581 582
#endif
  return result;
}
H
Haojun Liao 已提交
583

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

wafwerar's avatar
wafwerar 已提交
586 587 588 589 590 591 592 593 594 595
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;

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