ttime.c 25.0 KB
Newer Older
H
hzcheng 已提交
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/>.
 */

16
#ifdef DARWIN
H
hzcheng 已提交
17
#define _XOPEN_SOURCE
18 19 20 21
#else
#define _XOPEN_SOURCE 500
#endif

S
common  
Shengliang Guan 已提交
22
#define _BSD_SOURCE
S
slguan 已提交
23
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
24
#include "ttime.h"
S
Shengliang Guan 已提交
25

L
lihui 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * mktime64 - Converts date to seconds.
 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 *
 * [For the Julian calendar (which was used in Russia before 1917,
 * Britain & colonies before 1752, anywhere else before 1582,
 * and is still in use by some communities) leave out the
 * -year/100+year/400 terms, and add 10.]
 *
 * This algorithm was first published by Gauss (I think).
 *
 * A leap second can be indicated by calling this function with sec as
 * 60 (allowable under ISO 8601).  The leap second is treated the same
 * as the following second since they don't exist in UNIX time.
 *
 * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
 * tomorrow - (allowable under ISO 8601) is supported.
 */
S
Shengliang Guan 已提交
46 47 48
static int64_t user_mktime64(const uint32_t year0, const uint32_t mon0, const uint32_t day, const uint32_t hour,
                             const uint32_t min, const uint32_t sec, int64_t time_zone) {
  uint32_t mon = mon0, year = year0;
L
lihui 已提交
49

H
Haojun Liao 已提交
50
  /* 1..12 -> 11,12,1..10 */
S
Shengliang Guan 已提交
51 52
  if (0 >= (int32_t)(mon -= 2)) {
    mon += 12; /* Puts Feb last since it has leap day */
H
Haojun Liao 已提交
53 54
    year -= 1;
  }
L
lihui 已提交
55

S
Shengliang Guan 已提交
56 57
  // int64_t res = (((((int64_t) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  //                year*365 - 719499)*24 + hour)*60 + min)*60 + sec);
H
Hui Li 已提交
58
  int64_t res;
S
Shengliang Guan 已提交
59 60 61 62
  res = 367 * ((int64_t)mon) / 12;
  res += year / 4 - year / 100 + year / 400 + day + ((int64_t)year) * 365 - 719499;
  res = res * 24;
  res = ((res + hour) * 60 + min) * 60 + sec;
L
lihui 已提交
63

64
  return (res + time_zone);
L
lihui 已提交
65
}
66

L
lihui 已提交
67 68
// ==== mktime() kernel code =================//
static int64_t m_deltaUtc = 0;
S
Shengliang Guan 已提交
69
void           deltaToUtcInitOnce() {
L
lihui 已提交
70
  struct tm tm = {0};
71

wafwerar's avatar
wafwerar 已提交
72
  (void)taosStrpTime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm);
73
  m_deltaUtc = (int64_t)taosMktime(&tm);
S
Shengliang Guan 已提交
74
  // printf("====delta:%lld\n\n", seconds);
L
lihui 已提交
75 76
}

H
hzcheng 已提交
77
static int64_t parseFraction(char* str, char** end, int32_t timePrec);
78
static int32_t parseTimeWithTz(const char* timestr, int64_t* time, int32_t timePrec, char delim);
H
hzcheng 已提交
79
static int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec);
80
static int32_t parseLocaltimeDst(char* timestr, int64_t* time, int32_t timePrec);
S
Shengliang Guan 已提交
81 82
static char*   forwardToTimeStringEnd(char* str);
static bool    checkTzPresent(const char* str, int32_t len);
dengyihao's avatar
dengyihao 已提交
83

S
Shengliang Guan 已提交
84 85
static int32_t (*parseLocaltimeFp[])(char* timestr, int64_t* time, int32_t timePrec) = {parseLocaltime,
                                                                                        parseLocaltimeDst};
H
hzcheng 已提交
86

87
int32_t taosParseTime(const char* timestr, int64_t* time, int32_t len, int32_t timePrec, int8_t day_light) {
H
hzcheng 已提交
88
  /* parse datatime string in with tz */
S
slguan 已提交
89
  if (strnchr(timestr, 'T', len, false) != NULL) {
90
    return parseTimeWithTz(timestr, time, timePrec, 'T');
91 92
  } else if (checkTzPresent(timestr, len)) {
    return parseTimeWithTz(timestr, time, timePrec, 0);
H
hzcheng 已提交
93
  } else {
S
Shengliang Guan 已提交
94
    return (*parseLocaltimeFp[day_light])((char*)timestr, time, timePrec);
H
hzcheng 已提交
95 96 97
  }
}

S
Shengliang Guan 已提交
98 99
bool checkTzPresent(const char* str, int32_t len) {
  char*   seg = forwardToTimeStringEnd((char*)str);
100 101
  int32_t seg_len = len - (int32_t)(seg - str);

S
Shengliang Guan 已提交
102 103 104
  char* c = &seg[seg_len - 1];
  for (int32_t i = 0; i < seg_len; ++i) {
    if (*c == 'Z' || *c == 'z' || *c == '+' || *c == '-') {
105 106 107 108 109
      return true;
    }
    c--;
  }

110
  return false;
111 112
}

H
hzcheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
char* forwardToTimeStringEnd(char* str) {
  int32_t i = 0;
  int32_t numOfSep = 0;

  while (str[i] != 0 && numOfSep < 2) {
    if (str[i++] == ':') {
      numOfSep++;
    }
  }

  while (str[i] >= '0' && str[i] <= '9') {
    i++;
  }

  return &str[i];
}

int64_t parseFraction(char* str, char** end, int32_t timePrec) {
  int32_t i = 0;
  int64_t fraction = 0;

  const int32_t MILLI_SEC_FRACTION_LEN = 3;
  const int32_t MICRO_SEC_FRACTION_LEN = 6;
136
  const int32_t NANO_SEC_FRACTION_LEN = 9;
H
hzcheng 已提交
137

138
  int32_t factor[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
H
hzcheng 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  int32_t times = 1;

  while (str[i] >= '0' && str[i] <= '9') {
    i++;
  }

  int32_t totalLen = i;
  if (totalLen <= 0) {
    return -1;
  }

  /* parse the fraction */
  if (timePrec == TSDB_TIME_PRECISION_MILLI) {
    /* only use the initial 3 bits */
    if (i >= MILLI_SEC_FRACTION_LEN) {
      i = MILLI_SEC_FRACTION_LEN;
    }

    times = MILLI_SEC_FRACTION_LEN - i;
158
  } else if (timePrec == TSDB_TIME_PRECISION_MICRO) {
H
hzcheng 已提交
159 160 161 162
    if (i >= MICRO_SEC_FRACTION_LEN) {
      i = MICRO_SEC_FRACTION_LEN;
    }
    times = MICRO_SEC_FRACTION_LEN - i;
163 164 165 166 167 168
  } else {
    assert(timePrec == TSDB_TIME_PRECISION_NANO);
    if (i >= NANO_SEC_FRACTION_LEN) {
      i = NANO_SEC_FRACTION_LEN;
    }
    times = NANO_SEC_FRACTION_LEN - i;
H
hzcheng 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  }

  fraction = strnatoi(str, i) * factor[times];
  *end = str + totalLen;

  return fraction;
}

int32_t parseTimezone(char* str, int64_t* tzOffset) {
  int64_t hour = 0;

  int32_t i = 0;
  if (str[i] != '+' && str[i] != '-') {
    return -1;
  }

  i++;

D
dapan1121 已提交
187 188 189 190 191 192 193 194 195
  while (str[i]) {
    if ((str[i] >= '0' && str[i] <= '9') || str[i] == ':') {
      ++i;
      continue;
    }

    return -1;
  }

H
hzcheng 已提交
196 197
  char* sep = strchr(&str[i], ':');
  if (sep != NULL) {
S
slguan 已提交
198
    int32_t len = (int32_t)(sep - &str[i]);
H
hzcheng 已提交
199 200 201 202 203 204 205 206

    hour = strnatoi(&str[i], len);
    i += len + 1;
  } else {
    hour = strnatoi(&str[i], 2);
    i += 2;
  }

S
Shengliang Guan 已提交
207 208
  // return error if there're illegal charaters after min(2 Digits)
  char* minStr = &str[i];
209
  if (minStr[1] != '\0' && minStr[2] != '\0') {
S
Shengliang Guan 已提交
210
    return -1;
211 212
  }

weixin_48148422's avatar
weixin_48148422 已提交
213 214
  int64_t minute = strnatoi(&str[i], 2);
  if (minute > 59) {
H
hzcheng 已提交
215 216 217 218
    return -1;
  }

  if (str[0] == '+') {
weixin_48148422's avatar
weixin_48148422 已提交
219
    *tzOffset = -(hour * 3600 + minute * 60);
H
hzcheng 已提交
220
  } else {
weixin_48148422's avatar
weixin_48148422 已提交
221
    *tzOffset = hour * 3600 + minute * 60;
H
hzcheng 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  }

  return 0;
}

/*
 * rfc3339 format:
 * 2013-04-12T15:52:01+08:00
 * 2013-04-12T15:52:01.123+08:00
 *
 * 2013-04-12T15:52:01Z
 * 2013-04-12T15:52:01.123Z
 *
 * iso-8601 format:
 * 2013-04-12T15:52:01+0800
 * 2013-04-12T15:52:01.123+0800
 */
239
int32_t parseTimeWithTz(const char* timestr, int64_t* time, int32_t timePrec, char delim) {
S
Shengliang Guan 已提交
240 241
  int64_t factor =
      (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);
H
hzcheng 已提交
242 243 244
  int64_t tzOffset = 0;

  struct tm tm = {0};
245 246 247

  char* str;
  if (delim == 'T') {
wafwerar's avatar
wafwerar 已提交
248
    str = taosStrpTime(timestr, "%Y-%m-%dT%H:%M:%S", &tm);
249
  } else if (delim == 0) {
wafwerar's avatar
wafwerar 已提交
250
    str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm);
251 252 253 254
  } else {
    str = NULL;
  }

H
hzcheng 已提交
255 256 257 258
  if (str == NULL) {
    return -1;
  }

S
slguan 已提交
259 260
/* mktime will be affected by TZ, set by using taos_options */
#ifdef WINDOWS
S
Shengliang Guan 已提交
261 262
  int64_t seconds = user_mktime64(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
  // int64_t seconds = gmtime(&tm);
S
slguan 已提交
263
#else
H
hzcheng 已提交
264
  int64_t seconds = timegm(&tm);
S
slguan 已提交
265
#endif
H
hzcheng 已提交
266 267

  int64_t fraction = 0;
S
Shengliang Guan 已提交
268
  str = forwardToTimeStringEnd((char*)timestr);
H
hzcheng 已提交
269

270
  if ((str[0] == 'Z' || str[0] == 'z') && str[1] == '\0') {
H
hzcheng 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283
    /* utc time, no millisecond, return directly*/
    *time = seconds * factor;
  } else if (str[0] == '.') {
    str += 1;
    if ((fraction = parseFraction(str, &str, timePrec)) < 0) {
      return -1;
    }

    *time = seconds * factor + fraction;

    char seg = str[0];
    if (seg != 'Z' && seg != 'z' && seg != '+' && seg != '-') {
      return -1;
284 285
    } else if ((seg == 'Z' || seg == 'z') && str[1] != '\0') {
      return -1;
H
hzcheng 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    } else if (seg == '+' || seg == '-') {
      // parse the timezone
      if (parseTimezone(str, &tzOffset) == -1) {
        return -1;
      }

      *time += tzOffset * factor;
    }

  } else if (str[0] == '+' || str[0] == '-') {
    *time = seconds * factor + fraction;

    // parse the timezone
    if (parseTimezone(str, &tzOffset) == -1) {
      return -1;
    }

    *time += tzOffset * factor;
  } else {
    return -1;
  }

  return 0;
}

int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec) {
  *time = 0;
  struct tm tm = {0};

wafwerar's avatar
wafwerar 已提交
315
  char* str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm);
H
hzcheng 已提交
316 317 318 319
  if (str == NULL) {
    return -1;
  }

320 321 322 323 324 325
#ifdef _MSC_VER
#if _MSC_VER >= 1900
  int64_t timezone = _timezone;
#endif
#endif

S
Shengliang Guan 已提交
326 327 328
  int64_t seconds =
      user_mktime64(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, timezone);

H
hzcheng 已提交
329 330 331 332 333 334 335 336 337
  int64_t fraction = 0;

  if (*str == '.') {
    /* parse the second fraction part */
    if ((fraction = parseFraction(str + 1, &str, timePrec)) < 0) {
      return -1;
    }
  }

S
Shengliang Guan 已提交
338 339
  int64_t factor =
      (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);
H
hzcheng 已提交
340 341 342 343 344
  *time = factor * seconds + fraction;

  return 0;
}

345
int32_t parseLocaltimeDst(char* timestr, int64_t* time, int32_t timePrec) {
dengyihao's avatar
dengyihao 已提交
346 347 348 349
  *time = 0;
  struct tm tm = {0};
  tm.tm_isdst = -1;

wafwerar's avatar
wafwerar 已提交
350
  char* str = taosStrpTime(timestr, "%Y-%m-%d %H:%M:%S", &tm);
dengyihao's avatar
dengyihao 已提交
351 352 353 354 355
  if (str == NULL) {
    return -1;
  }

  /* mktime will be affected by TZ, set by using taos_options */
356
  int64_t seconds = taosMktime(&tm);
S
Shengliang Guan 已提交
357

dengyihao's avatar
dengyihao 已提交
358 359 360 361 362 363 364 365 366
  int64_t fraction = 0;

  if (*str == '.') {
    /* parse the second fraction part */
    if ((fraction = parseFraction(str + 1, &str, timePrec)) < 0) {
      return -1;
    }
  }

S
Shengliang Guan 已提交
367 368
  int64_t factor =
      (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);
dengyihao's avatar
dengyihao 已提交
369 370 371
  *time = factor * seconds + fraction;
  return 0;
}
B
Bomin Zhang 已提交
372

D
dapan1121 已提交
373 374 375 376 377 378 379 380 381 382 383 384
char getPrecisionUnit(int32_t precision) {
  static char units[3] = {TIME_UNIT_MILLISECOND, TIME_UNIT_MICROSECOND, TIME_UNIT_NANOSECOND};
  switch (precision) {
    case TSDB_TIME_PRECISION_MILLI:
    case TSDB_TIME_PRECISION_MICRO:
    case TSDB_TIME_PRECISION_NANO:
      return units[precision];
    default:
      return 0;
  }
}

385
int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision) {
386 387
  assert(fromPrecision == TSDB_TIME_PRECISION_MILLI ||
         fromPrecision == TSDB_TIME_PRECISION_MICRO ||
388
         fromPrecision == TSDB_TIME_PRECISION_NANO);
389 390
  assert(toPrecision == TSDB_TIME_PRECISION_MILLI ||
         toPrecision == TSDB_TIME_PRECISION_MICRO ||
391
         toPrecision == TSDB_TIME_PRECISION_NANO);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  double tempResult = (double)time;
  switch(fromPrecision) {
    case TSDB_TIME_PRECISION_MILLI: {
      switch (toPrecision) {
        case TSDB_TIME_PRECISION_MILLI:
          return time;
        case TSDB_TIME_PRECISION_MICRO:
          tempResult *= 1000;
          time *= 1000;
          goto end_;
        case TSDB_TIME_PRECISION_NANO:
          tempResult *= 1000000;
          time *= 1000000;
          goto end_;
      }
    } // end from milli
    case TSDB_TIME_PRECISION_MICRO: {
      switch (toPrecision) {
        case TSDB_TIME_PRECISION_MILLI:
          return time / 1000;
        case TSDB_TIME_PRECISION_MICRO:
          return time;
        case TSDB_TIME_PRECISION_NANO:
          tempResult *= 1000;
          time *= 1000;
          goto end_;
      }
    } //end from micro
    case TSDB_TIME_PRECISION_NANO: {
      switch (toPrecision) {
        case TSDB_TIME_PRECISION_MILLI:
          return time / 1000000;
        case TSDB_TIME_PRECISION_MICRO:
          return time / 1000;
        case TSDB_TIME_PRECISION_NANO:
          return time;
      }
    } //end from nano
    default: {
      assert(0);
      return time;  // only to pass windows compilation
    }
  } //end switch fromPrecision
end_:
  if (tempResult >= (double)INT64_MAX) return INT64_MAX;
  if (tempResult <= (double)INT64_MIN) return INT64_MIN;  // INT64_MIN means NULL
  return time;
439
}
440

441 442 443 444 445 446 447 448 449 450
// !!!!notice:there are precision problems, double lose precison if time is too large, for example: 1626006833631000000*1.0 = double = 1626006833631000064
//int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision) {
//  assert(fromPrecision == TSDB_TIME_PRECISION_MILLI || fromPrecision == TSDB_TIME_PRECISION_MICRO ||
//         fromPrecision == TSDB_TIME_PRECISION_NANO);
//  assert(toPrecision == TSDB_TIME_PRECISION_MILLI || toPrecision == TSDB_TIME_PRECISION_MICRO ||
//         toPrecision == TSDB_TIME_PRECISION_NANO);
//  static double factors[3][3] = {{1., 1000., 1000000.}, {1.0 / 1000, 1., 1000.}, {1.0 / 1000000, 1.0 / 1000, 1.}};
//  ((double)time * factors[fromPrecision][toPrecision]);
//}

451 452

// !!!!notice: double lose precison if time is too large, for example: 1626006833631000000*1.0 = double = 1626006833631000064
D
dapan1121 已提交
453 454 455
int64_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit) {
  assert(fromPrecision == TSDB_TIME_PRECISION_MILLI || fromPrecision == TSDB_TIME_PRECISION_MICRO ||
         fromPrecision == TSDB_TIME_PRECISION_NANO);
456 457
  int64_t factors[3] = {NANOSECOND_PER_MSEC, NANOSECOND_PER_USEC, 1};
  double tmp = time;
D
dapan1121 已提交
458
  switch (toUnit) {
459
    case 's':{
460 461
      tmp /= (NANOSECOND_PER_SEC/factors[fromPrecision]);     // the result of division is an integer
      time /= (NANOSECOND_PER_SEC/factors[fromPrecision]);
462 463
      break;
    }
D
dapan1121 已提交
464
    case 'm':
465 466
      tmp /= (NANOSECOND_PER_MINUTE/factors[fromPrecision]);  // the result of division is an integer
      time /= (NANOSECOND_PER_MINUTE/factors[fromPrecision]);
467
      break;
D
dapan1121 已提交
468
    case 'h':
469 470
      tmp /= (NANOSECOND_PER_HOUR/factors[fromPrecision]);    // the result of division is an integer
      time /= (NANOSECOND_PER_HOUR/factors[fromPrecision]);
471
      break;
D
dapan1121 已提交
472
    case 'd':
473 474
      tmp /= (NANOSECOND_PER_DAY/factors[fromPrecision]);     // the result of division is an integer
      time /= (NANOSECOND_PER_DAY/factors[fromPrecision]);
475
      break;
D
dapan1121 已提交
476
    case 'w':
477 478
      tmp /= (NANOSECOND_PER_WEEK/factors[fromPrecision]);    // the result of division is an integer
      time /= (NANOSECOND_PER_WEEK/factors[fromPrecision]);
479
      break;
D
dapan1121 已提交
480
    case 'a':
481 482
      tmp /= (NANOSECOND_PER_MSEC/factors[fromPrecision]);    // the result of division is an integer
      time /= (NANOSECOND_PER_MSEC/factors[fromPrecision]);
483
      break;
D
dapan1121 已提交
484
    case 'u':
485 486 487
      // the result of (NANOSECOND_PER_USEC/(double)factors[fromPrecision]) maybe a double
      switch (fromPrecision) {
        case TSDB_TIME_PRECISION_MILLI:{
488 489
          tmp *= 1000;
          time *= 1000;
490 491 492 493 494 495 496 497
          break;
        }
        case TSDB_TIME_PRECISION_MICRO:{
          tmp /= 1;
          time /= 1;
          break;
        }
        case TSDB_TIME_PRECISION_NANO:{
498 499
          tmp /= 1000;
          time /= 1000;
500 501 502 503
          break;
        }
      }
      break;
D
dapan1121 已提交
504
    case 'b':
505 506 507
      tmp *= factors[fromPrecision];
      time *= factors[fromPrecision];
      break;
D
dapan1121 已提交
508 509 510
    default: {
      return -1;
    }
511
  }
512 513 514
  if (tmp >= (double)INT64_MAX) return INT64_MAX;
  if (tmp <= (double)INT64_MIN) return INT64_MIN;
  return time;
515 516 517 518 519
}

int32_t convertStringToTimestamp(int16_t type, char *inputData, int64_t timePrec, int64_t *timeVal) {
  int32_t charLen = varDataLen(inputData);
  char *newColData;
X
Xiaoyu Wang 已提交
520
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY) {
521 522
    newColData = taosMemoryCalloc(1,  charLen + 1);
    memcpy(newColData, varDataVal(inputData), charLen);
523
    bool ret = taosParseTime(newColData, timeVal, charLen, (int32_t)timePrec, tsDaylight);
524 525 526 527
    if (ret != TSDB_CODE_SUCCESS) {
      taosMemoryFree(newColData);
      return ret;
    }
528 529 530 531 532 533 534 535 536
    taosMemoryFree(newColData);
  } else if (type == TSDB_DATA_TYPE_NCHAR) {
    newColData = taosMemoryCalloc(1,  charLen / TSDB_NCHAR_SIZE + 1);
    int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inputData), charLen, newColData);
    if (len < 0){
      taosMemoryFree(newColData);
      return TSDB_CODE_FAILED;
    }
    newColData[len] = 0;
537
    bool ret = taosParseTime(newColData, timeVal, len + 1, (int32_t)timePrec, tsDaylight);
538 539 540 541
    if (ret != TSDB_CODE_SUCCESS) {
      taosMemoryFree(newColData);
      return ret;
    }
542 543 544 545 546
    taosMemoryFree(newColData);
  } else {
    return TSDB_CODE_FAILED;
  }
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
547 548
}

549
static int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecision) {
H
hzcheng 已提交
550 551
  switch (unit) {
    case 's':
552
      (*result) = convertTimePrecision(val * MILLISECOND_PER_SECOND, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
hzcheng 已提交
553 554
      break;
    case 'm':
555
      (*result) = convertTimePrecision(val * MILLISECOND_PER_MINUTE, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
hzcheng 已提交
556 557
      break;
    case 'h':
558
      (*result) = convertTimePrecision(val * MILLISECOND_PER_HOUR, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
hzcheng 已提交
559 560
      break;
    case 'd':
561
      (*result) = convertTimePrecision(val * MILLISECOND_PER_DAY, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
hzcheng 已提交
562 563
      break;
    case 'w':
564
      (*result) = convertTimePrecision(val * MILLISECOND_PER_WEEK, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
hzcheng 已提交
565 566
      break;
    case 'a':
567
      (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_MILLI, timePrecision);
H
Haojun Liao 已提交
568 569
      break;
    case 'u':
570 571 572 573
      (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_MICRO, timePrecision);
      break;
    case 'b':
      (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_NANO, timePrecision);
H
hzcheng 已提交
574 575 576 577 578 579 580 581 582
      break;
    default: {
      return -1;
    }
  }
  return 0;
}

/*
583 584 585 586
 * n - months
 * y - Years
 * is not allowed, since the duration of month or year are both variable.
 *
587 588
 * b - nanoseconds;
 * u - microseconds;
H
hzcheng 已提交
589 590 591 592 593 594 595
 * a - Millionseconds
 * s - Seconds
 * m - Minutes
 * h - Hours
 * d - Days (24 hours)
 * w - Weeks (7 days)
 */
S
Shengliang Guan 已提交
596 597
int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* duration, char* unit,
                              int32_t timePrecision) {
H
hzcheng 已提交
598 599 600 601
  errno = 0;
  char* endPtr = NULL;

  /* get the basic numeric value */
wafwerar's avatar
wafwerar 已提交
602
  int64_t timestamp = taosStr2Int64(token, &endPtr, 10);
H
hzcheng 已提交
603 604 605 606
  if (errno != 0) {
    return -1;
  }

607
  /* natual month/year are not allowed in absolute duration */
608 609
  *unit = token[tokenlen - 1];
  if (*unit == 'n' || *unit == 'y') {
610 611 612
    return -1;
  }

613
  return getDuration(timestamp, *unit, duration, timePrecision);
H
hzcheng 已提交
614
}
615

616
int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision) {
B
Bomin Zhang 已提交
617 618 619
  errno = 0;

  /* get the basic numeric value */
wafwerar's avatar
wafwerar 已提交
620
  *duration = taosStr2Int64(token, NULL, 10);
B
Bomin Zhang 已提交
621 622 623 624 625 626 627 628 629
  if (errno != 0) {
    return -1;
  }

  *unit = token[tokenLen - 1];
  if (*unit == 'n' || *unit == 'y') {
    return 0;
  }

630
  return getDuration(*duration, *unit, duration, timePrecision);
B
Bomin Zhang 已提交
631 632
}

633 634 635 636
int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) {
  if (duration == 0) {
    return t;
  }
H
Haojun Liao 已提交
637 638

  if (unit != 'n' && unit != 'y') {
639 640 641
    return t + duration;
  }

H
Haojun Liao 已提交
642 643 644 645 646 647
  // The following code handles the y/n time duration
  int64_t numOfMonth = duration;
  if (unit == 'y') {
    numOfMonth *= 12;
  }

648
  struct tm tm;
S
Shengliang Guan 已提交
649
  time_t    tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision));
wafwerar's avatar
wafwerar 已提交
650
  taosLocalTime(&tt, &tm);
H
Haojun Liao 已提交
651
  int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)numOfMonth;
652 653 654
  tm.tm_year = mon / 12;
  tm.tm_mon = mon % 12;

655
  return (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision));
B
Bomin Zhang 已提交
656 657 658 659 660 661 662
}

int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision) {
  if (ekey < skey) {
    int64_t tmp = ekey;
    ekey = skey;
    skey = tmp;
663
  }
B
Bomin Zhang 已提交
664 665 666 667
  if (unit != 'n' && unit != 'y') {
    return (int32_t)((ekey - skey) / interval);
  }

S
Shengliang Guan 已提交
668 669
  skey /= (int64_t)(TSDB_TICK_PER_SECOND(precision));
  ekey /= (int64_t)(TSDB_TICK_PER_SECOND(precision));
670

B
Bomin Zhang 已提交
671
  struct tm tm;
S
Shengliang Guan 已提交
672
  time_t    t = (time_t)skey;
wafwerar's avatar
wafwerar 已提交
673
  taosLocalTime(&t, &tm);
S
Shengliang Guan 已提交
674
  int32_t smon = tm.tm_year * 12 + tm.tm_mon;
B
Bomin Zhang 已提交
675 676

  t = (time_t)ekey;
wafwerar's avatar
wafwerar 已提交
677
  taosLocalTime(&t, &tm);
S
Shengliang Guan 已提交
678
  int32_t emon = tm.tm_year * 12 + tm.tm_mon;
B
Bomin Zhang 已提交
679 680 681 682 683 684

  if (unit == 'y') {
    interval *= 12;
  }

  return (emon - smon) / (int32_t)interval;
685 686 687 688 689 690 691 692 693
}

int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision) {
  if (pInterval->sliding == 0) {
    assert(pInterval->interval == 0);
    return t;
  }

  int64_t start = t;
B
Bomin Zhang 已提交
694
  if (pInterval->slidingUnit == 'n' || pInterval->slidingUnit == 'y') {
S
Shengliang Guan 已提交
695
    start /= (int64_t)(TSDB_TICK_PER_SECOND(precision));
696
    struct tm tm;
S
Shengliang Guan 已提交
697
    time_t    tt = (time_t)start;
wafwerar's avatar
wafwerar 已提交
698
    taosLocalTime(&tt, &tm);
699 700 701 702 703 704 705
    tm.tm_sec = 0;
    tm.tm_min = 0;
    tm.tm_hour = 0;
    tm.tm_mday = 1;

    if (pInterval->slidingUnit == 'y') {
      tm.tm_mon = 0;
S
Shengliang Guan 已提交
706
      tm.tm_year = (int32_t)(tm.tm_year / pInterval->sliding * pInterval->sliding);
707
    } else {
S
Shengliang Guan 已提交
708 709
      int32_t mon = tm.tm_year * 12 + tm.tm_mon;
      mon = (int32_t)(mon / pInterval->sliding * pInterval->sliding);
710 711 712 713
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }

714
    start = (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision));
715 716
  } else {
    int64_t delta = t - pInterval->interval;
H
Haojun Liao 已提交
717
    int32_t factor = (delta >= 0) ? 1 : -1;
718 719 720 721

    start = (delta / pInterval->sliding + factor) * pInterval->sliding;

    if (pInterval->intervalUnit == 'd' || pInterval->intervalUnit == 'w') {
S
Shengliang Guan 已提交
722 723 724 725
      /*
       * here we revised the start time of day according to the local time zone,
       * but in case of DST, the start time of one day need to be dynamically decided.
       */
726
      // todo refactor to extract function that is available for Linux/Windows/Mac platform
S
Shengliang Guan 已提交
727
#if defined(WINDOWS) && _MSC_VER >= 1900
728 729 730 731
      // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019
      int64_t timezone = _timezone;
      int32_t daylight = _daylight;
      char**  tzname = _tzname;
S
Shengliang Guan 已提交
732
#endif
733

S
Shengliang Guan 已提交
734
      start += (int64_t)(timezone * TSDB_TICK_PER_SECOND(precision));
735 736
    }

H
Haojun Liao 已提交
737 738 739
    int64_t end = 0;

    // not enough time range
D
fix bug  
dapan1121 已提交
740
    if (start < 0 || INT64_MAX - start > pInterval->interval - 1) {
741
      end = taosTimeAdd(start, pInterval->interval, pInterval->intervalUnit, precision) - 1;
S
Shengliang Guan 已提交
742
      while (end < t && ((start + pInterval->sliding) <= INT64_MAX)) {  // move forward to the correct time window
H
Haojun Liao 已提交
743 744
        start += pInterval->sliding;

D
fix bug  
dapan1121 已提交
745
        if (start < 0 || INT64_MAX - start > pInterval->interval - 1) {
H
Haojun Liao 已提交
746 747 748 749 750 751 752 753
          end = start + pInterval->interval - 1;
        } else {
          end = INT64_MAX;
          break;
        }
      }
    } else {
      end = INT64_MAX;
754 755 756
    }
  }

H
Haojun Liao 已提交
757 758
  ASSERT(pInterval->offset >= 0);

B
Bomin Zhang 已提交
759 760 761 762
  if (pInterval->offset > 0) {
    start = taosTimeAdd(start, pInterval->offset, pInterval->offsetUnit, precision);
    if (start > t) {
      start = taosTimeAdd(start, -pInterval->interval, pInterval->intervalUnit, precision);
H
Haojun Liao 已提交
763 764
    } else {
      // try to move current window to the left-hande-side, due to the offset effect.
H
Haojun Liao 已提交
765
      int64_t end = taosTimeAdd(start, pInterval->interval, pInterval->intervalUnit, precision) - 1;
H
Haojun Liao 已提交
766 767 768 769 770
      ASSERT(end >= t);
      end = taosTimeAdd(end, -pInterval->sliding, pInterval->slidingUnit, precision);
      if (end >= t) {
        start = taosTimeAdd(start, -pInterval->sliding, pInterval->slidingUnit, precision);
      }
B
Bomin Zhang 已提交
771 772
    }
  }
H
Haojun Liao 已提交
773

B
Bomin Zhang 已提交
774
  return start;
775 776
}

777 778 779 780 781 782 783 784
// internal function, when program is paused in debugger,
// one can call this function from debugger to print a
// timestamp as human readable string, for example (gdb):
//     p fmtts(1593769722)
// outputs:
//     2020-07-03 17:48:42
// and the parameter can also be a variable.
const char* fmtts(int64_t ts) {
B
Bomin Zhang 已提交
785
  static char buf[96];
S
Shengliang Guan 已提交
786 787
  size_t      pos = 0;
  struct tm   tm;
788 789

  if (ts > -62135625943 && ts < 32503651200) {
B
Bomin Zhang 已提交
790
    time_t t = (time_t)ts;
wafwerar's avatar
wafwerar 已提交
791
    taosLocalTime(&t, &tm);
B
Bomin Zhang 已提交
792
    pos += strftime(buf + pos, sizeof(buf), "s=%Y-%m-%d %H:%M:%S", &tm);
793 794
  }

B
Bomin Zhang 已提交
795 796
  if (ts > -62135625943000 && ts < 32503651200000) {
    time_t t = (time_t)(ts / 1000);
wafwerar's avatar
wafwerar 已提交
797
    taosLocalTime(&t, &tm);
B
Bomin Zhang 已提交
798 799 800 801 802 803
    if (pos > 0) {
      buf[pos++] = ' ';
      buf[pos++] = '|';
      buf[pos++] = ' ';
    }
    pos += strftime(buf + pos, sizeof(buf), "ms=%Y-%m-%d %H:%M:%S", &tm);
S
Shengliang Guan 已提交
804
    pos += sprintf(buf + pos, ".%03d", (int32_t)(ts % 1000));
B
Bomin Zhang 已提交
805
  }
806

B
Bomin Zhang 已提交
807 808
  {
    time_t t = (time_t)(ts / 1000000);
wafwerar's avatar
wafwerar 已提交
809
    taosLocalTime(&t, &tm);
B
Bomin Zhang 已提交
810 811 812 813 814 815
    if (pos > 0) {
      buf[pos++] = ' ';
      buf[pos++] = '|';
      buf[pos++] = ' ';
    }
    pos += strftime(buf + pos, sizeof(buf), "us=%Y-%m-%d %H:%M:%S", &tm);
S
Shengliang Guan 已提交
816
    pos += sprintf(buf + pos, ".%06d", (int32_t)(ts % 1000000));
817 818 819
  }

  return buf;
820
}
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

void taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precision) {
  char       ts[40] = {0};
  struct tm* ptm;

  int32_t fractionLen;
  char*   format = NULL;
  time_t  quot = 0;
  long    mod = 0;

  switch (precision) {
    case TSDB_TIME_PRECISION_MILLI: {
      quot = t / 1000;
      fractionLen = 5;
      format = ".%03" PRId64;
      mod = t % 1000;
      break;
    }

    case TSDB_TIME_PRECISION_MICRO: {
      quot = t / 1000000;
      fractionLen = 8;
      format = ".%06" PRId64;
      mod = t % 1000000;
      break;
    }

    case TSDB_TIME_PRECISION_NANO: {
      quot = t / 1000000000;
      fractionLen = 11;
      format = ".%09" PRId64;
      mod = t % 1000000000;
      break;
    }

    default:
      fractionLen = 0;
      assert(false);
  }

861
  ptm = taosLocalTime(&quot, NULL);
862 863 864 865 866
  int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", ptm);
  length += snprintf(ts + length, fractionLen, format, mod);
  length += (int32_t)strftime(ts + length, 40 - length, "%z", ptm);

  tstrncpy(buf, ts, bufLen);
D
dapan1121 已提交
867
}