ttime.h 2.7 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 43 44 45 46 47 48
/*
 * @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.
 */
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 已提交
49
  } else {
S
Shengliang Guan 已提交
50 51 52 53 54 55 56 57
    return taosGetTimestampMs();
  }
}

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

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

61
int32_t taosParseTime(const char* timestr, int64_t* time, int32_t len, int32_t timePrec, int8_t dayligth);
S
Shengliang Guan 已提交
62
void    deltaToUtcInitOnce();
D
dapan1121 已提交
63
char getPrecisionUnit(int32_t precision);
S
Shengliang Guan 已提交
64 65

int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision);
D
dapan1121 已提交
66
int64_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit);
S
Shengliang Guan 已提交
67

68 69
void taosFormatUtcTime(char *buf, int32_t bufLen, int64_t time, int32_t precision);

S
Shengliang Guan 已提交
70 71 72 73
#ifdef __cplusplus
}
#endif

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