diff --git a/include/common/ttime.h b/include/common/ttime.h index 15450c31ca09998cc7ce22853872eff718917a25..3de0b98d85217f142a903c6e42c257bba5f299b9 100644 --- a/include/common/ttime.h +++ b/include/common/ttime.h @@ -40,6 +40,7 @@ extern "C" { * @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. + * precision == TSDB_TIME_PRECISION_NANO, it returns timestamp in nanosecond. */ static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { if (precision == TSDB_TIME_PRECISION_MICRO) { @@ -51,6 +52,24 @@ static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { } } +/* + * @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; +} + 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); diff --git a/include/os/osTime.h b/include/os/osTime.h index 766fec0fbd9c1a74be89e87fb74bf4d17a0e3453..fd431f6df8e65e952ce63d54a31a54c32432dd27 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -27,9 +27,11 @@ extern "C" { #ifndef ALLOW_FORBID_FUNC #define strptime STRPTIME_FUNC_TAOS_FORBID #define gettimeofday GETTIMEOFDAY_FUNC_TAOS_FORBID + #define localtime LOCALTIME_FUNC_TAOS_FORBID #define localtime_s LOCALTIMES_FUNC_TAOS_FORBID #define localtime_r LOCALTIMER_FUNC_TAOS_FORBID #define time TIME_FUNC_TAOS_FORBID + #define mktime MKTIME_FUNC_TAOS_FORBID #endif #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) @@ -82,6 +84,8 @@ static FORCE_INLINE int64_t taosGetTimestampNs() { char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm); struct tm *taosLocalTime(const time_t *timep, struct tm *result); +time_t taosTime(time_t *t); +time_t taosMktime(struct tm *timep); #ifdef __cplusplus } diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index dbe78782f5dd66284167a89fb731d944ab519fa0..3e95ba072b9c60f99bfba4bb2840df150e64fd5a 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -764,7 +764,7 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { } } - struct tm* ptm = localtime(&tt); + struct tm* ptm = taosLocalTime(&tt, NULL); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm); if (precision == TSDB_TIME_PRECISION_NANO) { diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 053a1a1ee6a802da00350cc08b429191d6ffd894..dad5805c6698b1c43920e9e4f854ab01d8217e29 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -1368,7 +1368,7 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { } } - struct tm* ptm = localtime(&tt); + struct tm* ptm = taosLocalTime(&tt, NULL); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm); if (precision == TSDB_TIME_PRECISION_NANO) { diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 1baf393e9a215c9e18cd2208c28a6163a501564b..4686d856cc36ebb1ecf71b7eda17b16c388fea87 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -70,7 +70,7 @@ void deltaToUtcInitOnce() { struct tm tm = {0}; (void)taosStrpTime("1970-01-01 00:00:00", (const char*)("%Y-%m-%d %H:%M:%S"), &tm); - m_deltaUtc = (int64_t)mktime(&tm); + m_deltaUtc = (int64_t)taosMktime(&tm); // printf("====delta:%lld\n\n", seconds); } @@ -344,7 +344,7 @@ int32_t parseLocaltimeDst(char* timestr, int64_t* time, int32_t timePrec) { } /* mktime will be affected by TZ, set by using taos_options */ - int64_t seconds = mktime(&tm); + int64_t seconds = taosMktime(&tm); int64_t fraction = 0; @@ -539,7 +539,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - return (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision)); + return (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision)); } int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision) { @@ -598,7 +598,7 @@ int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precisio tm.tm_mon = mon % 12; } - start = (int64_t)(mktime(&tm) * TSDB_TICK_PER_SECOND(precision)); + start = (int64_t)(taosMktime(&tm) * TSDB_TICK_PER_SECOND(precision)); } else { int64_t delta = t - pInterval->interval; int32_t factor = (delta >= 0) ? 1 : -1; @@ -745,7 +745,7 @@ void taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precision) assert(false); } - ptm = localtime("); + ptm = taosLocalTime(", NULL); 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); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 0eac50782200c29bba13b19a75e7cb941118a18e..56d6bbd1866b72cb2b37c9d6cd2102eafdc3d3ed 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -173,12 +173,12 @@ static void getNextTimeWindow(SInterval* pInterval, int32_t precision, int32_t o int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor); tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - tw->skey = convertTimePrecision((int64_t)mktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, precision); + tw->skey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, precision); mon = (int)(mon + interval); tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - tw->ekey = convertTimePrecision((int64_t)mktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, precision); + tw->ekey = convertTimePrecision((int64_t)taosMktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, precision); tw->ekey -= 1; } diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 81831ab164187d996a441a085cec913509d586d5..9e53eee5c8d16ce329487aed3e0ab60e38c3824f 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -312,6 +312,8 @@ static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time if (pToken->type == TK_NOW) { ts = taosGetTimestamp(timePrec); + } else if (pToken->type == TK_TODAY) { + ts = taosGetTimestampToday(timePrec); } else if (pToken->type == TK_NK_INTEGER) { bool isSigned = false; toInteger(pToken->z, pToken->n, 10, &ts, &isSigned); @@ -376,8 +378,8 @@ static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time } static FORCE_INLINE int32_t checkAndTrimValue(SToken* pToken, uint32_t type, char* tmpTokenBuf, SMsgBuf* pMsgBuf) { - if ((pToken->type != TK_NOW && pToken->type != TK_NK_INTEGER && pToken->type != TK_NK_STRING && pToken->type != TK_NK_FLOAT && pToken->type != TK_NK_BOOL && - pToken->type != TK_NULL && pToken->type != TK_NK_HEX && pToken->type != TK_NK_OCT && pToken->type != TK_NK_BIN) || + if ((pToken->type != TK_NOW && pToken->type != TK_TODAY && pToken->type != TK_NK_INTEGER && pToken->type != TK_NK_STRING && pToken->type != TK_NK_FLOAT && + pToken->type != TK_NK_BOOL && pToken->type != TK_NULL && pToken->type != TK_NK_HEX && pToken->type != TK_NK_OCT && pToken->type != TK_NK_BIN) || (pToken->n == 0) || (pToken->type == TK_NK_RP)) { return buildSyntaxErrMsg(pMsgBuf, "invalid data or symbol", pToken->z); } diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index db62a6b33d6baaf60b4cc0c0fa0eda8bf641ef53..4d57af822ea9cc938d36b208febce7b4a3cd1903 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -841,7 +841,7 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam * memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS); } - struct tm *tmInfo = localtime((const time_t *)&timeVal); + struct tm *tmInfo = taosLocalTime((const time_t *)&timeVal, NULL); strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", tmInfo); int32_t len = (int32_t)strlen(buf); diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 2b0de948805c7a02ea98a849b8f75b7ff372d6e4..9ea49b364e902f48febe0f85f4cf90e8425fe6ad 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -406,7 +406,18 @@ FORCE_INLINE int32_t taosGetTimeOfDay(struct timeval *tv) { #endif } +time_t taosTime(time_t *t) { + return time(t); +} + +time_t taosMktime(struct tm *timep) { + return mktime(timep); +} + struct tm *taosLocalTime(const time_t *timep, struct tm *result) { + if (result == NULL) { + return localtime(timep); + } #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) localtime_s(result, timep); #else diff --git a/tests/test/c/tmqDemo.c b/tests/test/c/tmqDemo.c index 45a9d31f4cf09eb82f0962aba21c10335b1dd4b3..d71a9c1a95964c4b416f9bd8ceff9c5c19f2c645 100644 --- a/tests/test/c/tmqDemo.c +++ b/tests/test/c/tmqDemo.c @@ -596,7 +596,7 @@ void printParaIntoFile() { g_fp = pFile; time_t tTime = taosGetTimestampSec(); - struct tm tm = *localtime(&tTime); + struct tm tm = *taosLocalTime(&tTime, NULL); taosFprintfFile(pFile, "###################################################################\n"); taosFprintfFile(pFile, "# configDir: %s\n", configDir); diff --git a/tests/tsim/src/simExe.c b/tests/tsim/src/simExe.c index 8ef5c816c803d8c6b34b4c85f9244047dc1db20f..d71323336292cccf3d4cdb951c3ce457bded0667 100644 --- a/tests/tsim/src/simExe.c +++ b/tests/tsim/src/simExe.c @@ -678,7 +678,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { if (tt < 0) tt = 0; #endif - tp = localtime(&tt); + tp = taosLocalTime(&tt, NULL); strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", tp); if (precision == TSDB_TIME_PRECISION_MILLI) { sprintf(value, "%s.%03d", timeStr, (int32_t)(*((int64_t *)row[i]) % 1000)); diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index b89d517ad3e4396d1472e0b0a6a8a7d2333ab7c5..1809d99209b89fc77a7b1fdd7dd2dec13cc993c6 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -452,7 +452,7 @@ static char *formatTimestamp(char *buf, int64_t val, int precision) { } } - struct tm *ptm = localtime(&tt); + struct tm *ptm = taosLocalTime(&tt, NULL); size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm); if (precision == TSDB_TIME_PRECISION_NANO) {