ttime.h 3.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef _TD_COMMON_TIME_H_
#define _TD_COMMON_TIME_H_

S
Shengliang Guan 已提交
19 20 21
#include "taosdef.h"
#include "tmsg.h"

S
Shengliang Guan 已提交
22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

26 27
#define TIME_IS_VAR_DURATION(_t) ((_t) == 'n' || (_t) == 'y' || (_t) == 'N' || (_t) == 'Y')

X
Xiaoyu Wang 已提交
28 29 30 31 32 33 34 35 36 37 38
#define TIME_UNIT_NANOSECOND  'b'
#define TIME_UNIT_MICROSECOND 'u'
#define TIME_UNIT_MILLISECOND 'a'
#define TIME_UNIT_SECOND      's'
#define TIME_UNIT_MINUTE      'm'
#define TIME_UNIT_HOUR        'h'
#define TIME_UNIT_DAY         'd'
#define TIME_UNIT_WEEK        'w'
#define TIME_UNIT_MONTH       'n'
#define TIME_UNIT_YEAR        'y'

S
Shengliang Guan 已提交
39 40 41 42
/*
 * @return timestamp decided by global conf variable, tsTimePrecision
 * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond.
 *    precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond.
43
 *    precision == TSDB_TIME_PRECISION_NANO,  it returns timestamp in nanosecond.
S
Shengliang Guan 已提交
44 45 46 47 48 49
 */
static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) {
  if (precision == TSDB_TIME_PRECISION_MICRO) {
    return taosGetTimestampUs();
  } else if (precision == TSDB_TIME_PRECISION_NANO) {
    return taosGetTimestampNs();
S
Shengliang Guan 已提交
50
  } else {
S
Shengliang Guan 已提交
51 52 53 54
    return taosGetTimestampMs();
  }
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
 * @return timestamp of today at 00:00:00 in given precision
 * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond.
 *    precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond.
 *    precision == TSDB_TIME_PRECISION_NANO,  it returns timestamp in nanosecond.
 */
static FORCE_INLINE int64_t taosGetTimestampToday(int32_t precision) {
  int64_t factor = (precision == TSDB_TIME_PRECISION_MILLI) ? 1000 :
                   (precision == TSDB_TIME_PRECISION_MICRO) ? 1000000 : 1000000000;
  time_t t = taosTime(NULL);
  struct tm * tm= taosLocalTime(&t, NULL);
  tm->tm_hour = 0;
  tm->tm_min = 0;
  tm->tm_sec = 0;

  return (int64_t)taosMktime(tm) * factor;
}

S
Shengliang Guan 已提交
73 74 75 76
int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision);
int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision);
int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision);

77
int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* ts, char* unit, int32_t timePrecision);
S
Shengliang Guan 已提交
78 79
int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision);

80
int32_t taosParseTime(const char* timestr, int64_t* time, int32_t len, int32_t timePrec, int8_t dayligth);
S
Shengliang Guan 已提交
81
void    deltaToUtcInitOnce();
D
dapan1121 已提交
82
char getPrecisionUnit(int32_t precision);
S
Shengliang Guan 已提交
83 84

int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision);
D
dapan1121 已提交
85
int64_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit);
86
int32_t convertStringToTimestamp(int16_t type, char *inputData, int64_t timePrec, int64_t *timeVal);
S
Shengliang Guan 已提交
87

88 89
void taosFormatUtcTime(char *buf, int32_t bufLen, int64_t time, int32_t precision);

S
Shengliang Guan 已提交
90 91 92 93
#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
94
#endif /*_TD_COMMON_TIME_H_*/