提交 9a1da224 编写于 作者: B Benguang Zhao

enh: refactor taosTimeTruncate and taosTimeCountInterval

上级 d7f2d93a
...@@ -106,7 +106,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision); ...@@ -106,7 +106,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision);
int64_t taosTimeSub(int64_t t, int64_t duration, char unit, int32_t precision); int64_t taosTimeSub(int64_t t, int64_t duration, char unit, int32_t precision);
int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, 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); int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t sliding, int64_t slidingUnit, int64_t intervalUnit, int32_t precision);
int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* ts, char* unit, int32_t timePrecision); int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* ts, char* unit, int32_t timePrecision);
int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision); int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision);
......
...@@ -565,23 +565,29 @@ int64_t taosTimeSub(int64_t t, int64_t duration, char unit, int32_t precision) { ...@@ -565,23 +565,29 @@ int64_t taosTimeSub(int64_t t, int64_t duration, char unit, int32_t precision) {
return (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision)); return (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision));
} }
int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision) { int64_t taosTimeTzOffset(int64_t intervalUnit, int32_t precision) {
int64_t tz_offset = 0;
if (intervalUnit == 'd' || intervalUnit == 'w') {
#if defined(WINDOWS) && _MSC_VER >= 1900
// see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019
int64_t timezone = _timezone;
#endif
tz_offset = -1 * timezone * TSDB_TICK_PER_SECOND(precision);
}
return tz_offset;
}
int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t sliding, int64_t slidingUnit, int64_t intervalUnit, int32_t precision) {
if (ekey < skey) { if (ekey < skey) {
int64_t tmp = ekey; int64_t tmp = ekey;
ekey = skey; ekey = skey;
skey = tmp; skey = tmp;
} }
#ifdef _MSC_VER int64_t tz_offset = taosTimeTzOffset(intervalUnit, precision);
#if _MSC_VER >= 1900
int64_t timezone = _timezone; if (slidingUnit != 'n' && slidingUnit != 'y') {
#endif return (int32_t)((ekey+tz_offset)/sliding - (skey+tz_offset)/sliding) + 1;
#endif
int64_t tz_offset = -1 * timezone * TSDB_TICK_PER_SECOND(precision);
if (unit != 'n' && unit != 'y') {
return (int32_t)((ekey+tz_offset)/interval - (skey+tz_offset)/interval) + 1;
} }
skey /= (int64_t)(TSDB_TICK_PER_SECOND(precision)); skey /= (int64_t)(TSDB_TICK_PER_SECOND(precision));
...@@ -596,11 +602,11 @@ int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char ...@@ -596,11 +602,11 @@ int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char
localtime_r(&t, &tm); localtime_r(&t, &tm);
int emon = tm.tm_year * 12 + tm.tm_mon; int emon = tm.tm_year * 12 + tm.tm_mon;
if (unit == 'y') { if (slidingUnit == 'y') {
interval *= 12; sliding *= 12;
} }
return (int32_t)(emon/interval - smon/interval) + 1; return (int32_t)(emon/sliding - smon/sliding) + 1;
} }
int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision) { int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision) {
...@@ -622,56 +628,22 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio ...@@ -622,56 +628,22 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio
if (pInterval->slidingUnit == 'y') { if (pInterval->slidingUnit == 'y') {
tm.tm_mon = 0; tm.tm_mon = 0;
tm.tm_year = (int)(tm.tm_year / pInterval->sliding * pInterval->sliding); tm.tm_year -= (int)(tm.tm_year%pInterval->sliding);
} else { } else {
int mon = tm.tm_year * 12 + tm.tm_mon; int mon = tm.tm_year * 12 + tm.tm_mon;
mon = (int)(mon / pInterval->sliding * pInterval->sliding); mon -= (int)(mon % pInterval->sliding);
tm.tm_year = mon / 12; tm.tm_year = mon / 12;
tm.tm_mon = mon % 12; tm.tm_mon = mon % 12;
} }
start = (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision)); start = (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision));
} else { } else {
int64_t delta = t - pInterval->interval; // To avoid the overly complicated effect of DST on size of sliding windows, the DST(daylight saving time) is
int32_t factor = (delta >= 0) ? 1 : -1; // better to be respected by users' input and output for display only, maintaining the status quo.
// In this way, the size of sliding windows keeps unchanging during each cycle of processing of requests.
start = (delta / pInterval->sliding + factor) * pInterval->sliding; start = t - pInterval->interval + pInterval->sliding;
int64_t tz_offset = taosTimeTzOffset(pInterval->intervalUnit, precision);
if (pInterval->intervalUnit == 'd' || pInterval->intervalUnit == 'w') { start -= (start+tz_offset)%pInterval->sliding;
/*
* 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.
*/
// todo refactor to extract function that is available for Linux/Windows/Mac platform
#if defined(WINDOWS) && _MSC_VER >= 1900
// 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;
#endif
start += (int64_t)(timezone * TSDB_TICK_PER_SECOND(precision));
}
int64_t end = 0;
// not enough time range
if (start < 0 || INT64_MAX - start > pInterval->interval - 1) {
end = start + pInterval->interval - 1;
while(end < t && ((start + pInterval->sliding) <= INT64_MAX)) { // move forward to the correct time window
start += pInterval->sliding;
if (start < 0 || INT64_MAX - start > pInterval->interval - 1) {
end = start + pInterval->interval - 1;
} else {
end = INT64_MAX;
break;
}
}
} else {
end = INT64_MAX;
}
} }
if (pInterval->offset > 0) { if (pInterval->offset > 0) {
......
...@@ -462,6 +462,7 @@ void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput) ...@@ -462,6 +462,7 @@ void taosFillSetInputDataBlock(SFillInfo* pFillInfo, const SSDataBlock* pInput)
pFillInfo->currentKey, pFillInfo->currentKey,
pFillInfo->interval.sliding, pFillInfo->interval.sliding,
pFillInfo->interval.slidingUnit, pFillInfo->interval.slidingUnit,
pFillInfo->interval.intervalUnit,
pFillInfo->precision); pFillInfo->precision);
if(numOfRes < numOfRows || pFillInfo->currentKey < lastKey) { if(numOfRes < numOfRows || pFillInfo->currentKey < lastKey) {
// set currentKey max // set currentKey max
...@@ -502,6 +503,7 @@ int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t ma ...@@ -502,6 +503,7 @@ int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t ma
pFillInfo->currentKey, pFillInfo->currentKey,
pFillInfo->interval.sliding, pFillInfo->interval.sliding,
pFillInfo->interval.slidingUnit, pFillInfo->interval.slidingUnit,
pFillInfo->interval.intervalUnit,
pFillInfo->precision); pFillInfo->precision);
assert(numOfRes >= numOfRows); assert(numOfRes >= numOfRows);
} else { // reach the end of data } else { // reach the end of data
...@@ -514,6 +516,7 @@ int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t ma ...@@ -514,6 +516,7 @@ int64_t getNumOfResultsAfterFillGap(SFillInfo* pFillInfo, TSKEY ekey, int32_t ma
pFillInfo->currentKey, pFillInfo->currentKey,
pFillInfo->interval.sliding, pFillInfo->interval.sliding,
pFillInfo->interval.slidingUnit, pFillInfo->interval.slidingUnit,
pFillInfo->interval.intervalUnit,
pFillInfo->precision); pFillInfo->precision);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册