/src/tdengine/include/common/ttime.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * This program is free software: you can use, redistribute, and/or modify |
5 | | * it under the terms of the GNU Affero General Public License, version 3 |
6 | | * or later ("AGPL"), as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
9 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | | * FITNESS FOR A PARTICULAR PURPOSE. |
11 | | * |
12 | | * You should have received a copy of the GNU Affero General Public License |
13 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | | */ |
15 | | |
16 | | #ifndef _TD_COMMON_TIME_H_ |
17 | | #define _TD_COMMON_TIME_H_ |
18 | | |
19 | | #include "taosdef.h" |
20 | | #include "tmsg.h" |
21 | | |
22 | | #ifdef __cplusplus |
23 | | extern "C" { |
24 | | #endif |
25 | | |
26 | 0 | #define IS_CALENDAR_TIME_DURATION(_t) ((_t) == 'n' || (_t) == 'y' || (_t) == 'N' || (_t) == 'Y') |
27 | | |
28 | 0 | #define TIME_UNIT_NANOSECOND 'b' |
29 | 0 | #define TIME_UNIT_MICROSECOND 'u' |
30 | 0 | #define TIME_UNIT_MILLISECOND 'a' |
31 | 0 | #define TIME_UNIT_SECOND 's' |
32 | 0 | #define TIME_UNIT_MINUTE 'm' |
33 | 0 | #define TIME_UNIT_HOUR 'h' |
34 | 0 | #define TIME_UNIT_DAY 'd' |
35 | 0 | #define TIME_UNIT_WEEK 'w' |
36 | 0 | #define TIME_UNIT_MONTH 'n' |
37 | 0 | #define TIME_UNIT_YEAR 'y' |
38 | | |
39 | 0 | #define AUTO_DURATION_LITERAL "auto" |
40 | 0 | #define AUTO_DURATION_VALUE -1 |
41 | | |
42 | | /* |
43 | | * @return timestamp decided by global conf variable, tsTimePrecision |
44 | | * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond. |
45 | | * precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond. |
46 | | * precision == TSDB_TIME_PRECISION_NANO, it returns timestamp in nanosecond. |
47 | | */ |
48 | 0 | static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { |
49 | 0 | if (precision == TSDB_TIME_PRECISION_MICRO) { |
50 | 0 | return taosGetTimestampUs(); |
51 | 0 | } else if (precision == TSDB_TIME_PRECISION_NANO) { |
52 | 0 | return taosGetTimestampNs(); |
53 | 0 | } else { |
54 | 0 | return taosGetTimestampMs(); |
55 | 0 | } |
56 | 0 | } Unexecuted instantiation: parser.c:taosGetTimestamp Unexecuted instantiation: parTranslater.c:taosGetTimestamp Unexecuted instantiation: parCalcConst.c:taosGetTimestamp Unexecuted instantiation: parInsertSql.c:taosGetTimestamp Unexecuted instantiation: parAstCreater.c:taosGetTimestamp Unexecuted instantiation: parInsertSml.c:taosGetTimestamp Unexecuted instantiation: planOptimizer.c:taosGetTimestamp Unexecuted instantiation: builtinsimpl.c:taosGetTimestamp Unexecuted instantiation: filter.c:taosGetTimestamp Unexecuted instantiation: scalar.c:taosGetTimestamp Unexecuted instantiation: sclfunc.c:taosGetTimestamp Unexecuted instantiation: sclvector.c:taosGetTimestamp Unexecuted instantiation: tlog.c:taosGetTimestamp Unexecuted instantiation: ttime.c:taosGetTimestamp Unexecuted instantiation: tvariant.c:taosGetTimestamp |
57 | | |
58 | | /* |
59 | | * @return timestamp of today at 00:00:00 in given precision |
60 | | * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond. |
61 | | * precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond. |
62 | | * precision == TSDB_TIME_PRECISION_NANO, it returns timestamp in nanosecond. |
63 | | */ |
64 | | int64_t taosGetTimestampToday(int32_t precision, timezone_t tz); |
65 | | |
66 | | int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision, timezone_t tz); |
67 | | |
68 | | TSKEY getNextTimeWindowStart(const SInterval* pInterval, TSKEY start, int32_t order); |
69 | | int64_t taosTimeTruncate(int64_t ts, const SInterval* pInterval); |
70 | | int64_t taosTimeGetIntervalEnd(int64_t ts, const SInterval* pInterval); |
71 | | int32_t taosTimeCountIntervalForFill(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision, int32_t order); |
72 | | void calcIntervalAutoOffset(SInterval* interval); |
73 | | |
74 | | int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* ts, char* unit, int32_t timePrecision); |
75 | | int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision, bool negativeAllow); |
76 | | |
77 | | int32_t taosParseShortWeekday(const char* str); |
78 | | int32_t taosParseTime(const char* timestr, int64_t* pTime, int32_t len, int32_t timePrec, timezone_t tz); |
79 | | char getPrecisionUnit(int32_t precision); |
80 | | |
81 | | int64_t convertTimePrecision(int64_t ts, int32_t fromPrecision, int32_t toPrecision); |
82 | | int32_t convertCalendarTimeFromUnitToPrecision(int64_t time, char fromUnit, int32_t toPrecision,int64_t* pRes); |
83 | | int32_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit, int64_t* pRes); |
84 | | int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec, int64_t* timeVal, timezone_t tz, void* charsetCxt); |
85 | | int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecision); |
86 | | int64_t alignToNaturalBoundary(int64_t timestamp, char unit, int64_t value, int64_t offset, int32_t precision, timezone_t tz); |
87 | | |
88 | | int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t ts, int32_t precision); |
89 | | char* formatTimestampLocal(char* buf, int32_t cap, int64_t val, int precision); |
90 | | struct STm { |
91 | | struct tm tm; |
92 | | int64_t fsec; // in NANOSECOND |
93 | | }; |
94 | | |
95 | | int32_t taosTs2Tm(int64_t ts, int32_t precision, struct STm* tm, timezone_t tz); |
96 | | int32_t taosTm2Ts(struct STm* tm, int64_t* ts, int32_t precision, timezone_t tz); |
97 | | |
98 | | /// @brief convert a timestamp to a formatted string |
99 | | /// @param format the timestamp format, must null terminated |
100 | | /// @param [in,out] formats the formats array pointer generated. Shouldn't be NULL. |
101 | | /// If (*formats == NULL), [format] will be used and [formats] will be updated to the new generated |
102 | | /// formats array; If not NULL, [formats] will be used instead of [format] to skip parse formats again. |
103 | | /// @param out output buffer, should be initialized by memset |
104 | | /// @notes remember to free the generated formats |
105 | | int32_t taosTs2Char(const char* format, SArray** formats, int64_t ts, int32_t precision, char* out, int32_t outLen, timezone_t tz); |
106 | | /// @brief convert a formatted timestamp string to a timestamp |
107 | | /// @param format must null terminated |
108 | | /// @param [in, out] formats, see taosTs2Char |
109 | | /// @param tsStr must null terminated |
110 | | /// @retval 0 for success, otherwise error occured |
111 | | /// @notes remember to free the generated formats even when error occured |
112 | | int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int64_t* ts, int32_t precision, char* errMsg, |
113 | | int32_t errMsgLen, timezone_t tz); |
114 | | |
115 | | int32_t TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen); |
116 | | int32_t TEST_char2ts(const char* format, int64_t* ts, int32_t precision, const char* tsStr); |
117 | | |
118 | | /// @brief get offset seconds from zero timezone to input timezone |
119 | | /// for +XX timezone, the offset to zero is negative value |
120 | | /// @param tzStr timezonestr, eg: +0800, -0830, -08 |
121 | | /// @param offset seconds, eg: +08 offset -28800, -01 offset 3600 |
122 | | /// @return 0 success, other fail |
123 | | int32_t offsetOfTimezone(char* tzStr, int64_t* offset); |
124 | | |
125 | | bool checkRecursiveTsmaInterval(int64_t baseInterval, int8_t baseUnit, int64_t interval, int8_t unit, int8_t precision, |
126 | | bool checkEq); |
127 | | |
128 | | #ifdef __cplusplus |
129 | | } |
130 | | #endif |
131 | | |
132 | | #endif /*_TD_COMMON_TIME_H_*/ |