From 4b137f1ca0e65c7d583d8515fb9aa8f4a0d817cc Mon Sep 17 00:00:00 2001 From: jiacy-jcy <714897623@qq.com> Date: Sun, 6 Aug 2023 23:32:54 +0800 Subject: [PATCH 1/4] fix: mktime on windows platform --- source/os/src/osTime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 39d1de0437..a9965c57c2 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -368,7 +368,7 @@ int32_t taosGetTimeOfDay(struct timeval *tv) { time_t taosTime(time_t *t) { return time(t); } time_t taosMktime(struct tm *timep) { -#ifdef WINDOWS +#ifdef WINDOWS_STASH struct tm tm1 = {0}; LARGE_INTEGER t; FILETIME f; From 6fd2ae138e7ab2d67d6212d654ab2b9e98218244 Mon Sep 17 00:00:00 2001 From: jiacy-jcy <714897623@qq.com> Date: Mon, 7 Aug 2023 17:32:20 +0800 Subject: [PATCH 2/4] fix: taosMktime on windows platform --- include/os/osTime.h | 2 ++ source/common/src/ttime.c | 40 ------------------------------ source/os/src/osTime.c | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/include/os/osTime.h b/include/os/osTime.h index 51a285a139..87df3a2650 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -95,6 +95,8 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf); struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst); time_t taosTime(time_t *t); time_t taosMktime(struct tm *timep); +int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour, + const uint32_t min, const uint32_t sec, int64_t time_zone); #ifdef __cplusplus } diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 7a5581efbe..e9313e0591 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -25,46 +25,6 @@ #include "tlog.h" -/* - * mktime64 - Converts date to seconds. - * Converts Gregorian date to seconds since 1970-01-01 00:00:00. - * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 - * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. - * - * [For the Julian calendar (which was used in Russia before 1917, - * Britain & colonies before 1752, anywhere else before 1582, - * and is still in use by some communities) leave out the - * -year/100+year/400 terms, and add 10.] - * - * This algorithm was first published by Gauss (I think). - * - * A leap second can be indicated by calling this function with sec as - * 60 (allowable under ISO 8601). The leap second is treated the same - * as the following second since they don't exist in UNIX time. - * - * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight - * tomorrow - (allowable under ISO 8601) is supported. - */ -static int64_t user_mktime64(const uint32_t year0, const uint32_t mon0, const uint32_t day, const uint32_t hour, - const uint32_t min, const uint32_t sec, int64_t time_zone) { - uint32_t mon = mon0, year = year0; - - /* 1..12 -> 11,12,1..10 */ - if (0 >= (int32_t)(mon -= 2)) { - mon += 12; /* Puts Feb last since it has leap day */ - year -= 1; - } - - // int64_t res = (((((int64_t) (year/4 - year/100 + year/400 + 367*mon/12 + day) + - // year*365 - 719499)*24 + hour)*60 + min)*60 + sec); - int64_t res; - res = 367 * ((int64_t)mon) / 12; - res += year / 4 - year / 100 + year / 400 + day + ((int64_t)year) * 365 - 719499; - res = res * 24; - res = ((res + hour) * 60 + min) * 60 + sec; - - return (res + time_zone); -} // ==== mktime() kernel code =================// static int64_t m_deltaUtc = 0; diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 39d1de0437..0430dd70fc 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -367,8 +367,50 @@ int32_t taosGetTimeOfDay(struct timeval *tv) { time_t taosTime(time_t *t) { return time(t); } +/* + * mktime64 - Converts date to seconds. + * Converts Gregorian date to seconds since 1970-01-01 00:00:00. + * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 + * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. + * + * [For the Julian calendar (which was used in Russia before 1917, + * Britain & colonies before 1752, anywhere else before 1582, + * and is still in use by some communities) leave out the + * -year/100+year/400 terms, and add 10.] + * + * This algorithm was first published by Gauss (I think). + * + * A leap second can be indicated by calling this function with sec as + * 60 (allowable under ISO 8601). The leap second is treated the same + * as the following second since they don't exist in UNIX time. + * + * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight + * tomorrow - (allowable under ISO 8601) is supported. + */ +int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour, + const uint32_t min, const uint32_t sec, int64_t time_zone) { + uint32_t _mon = mon, _year = year; + + /* 1..12 -> 11,12,1..10 */ + if (0 >= (int32_t)(_mon -= 2)) { + _mon += 12; /* Puts Feb last since it has leap day */ + _year -= 1; + } + + // int64_t _res = (((((int64_t) (_year/4 - _year/100 + _year/400 + 367*_mon/12 + day) + + // _year*365 - 719499)*24 + hour)*60 + min)*60 + sec); + int64_t _res; + _res = 367 * ((int64_t)_mon) / 12; + _res += _year / 4 - _year / 100 + _year / 400 + day + ((int64_t)_year) * 365 - 719499; + _res = _res * 24; + _res = ((_res + hour) * 60 + min) * 60 + sec; + + return (_res + time_zone); +} + time_t taosMktime(struct tm *timep) { #ifdef WINDOWS +#if 0 struct tm tm1 = {0}; LARGE_INTEGER t; FILETIME f; @@ -405,6 +447,15 @@ time_t taosMktime(struct tm *timep) { t.QuadPart -= offset.QuadPart; return (time_t)(t.QuadPart / 10000000); +#else +#ifdef _MSC_VER +#if _MSC_VER >= 1900 + int64_t tz = _timezone; +#endif +#endif + return user_mktime64(timep->tm_year + 1900, timep->tm_mon + 1, timep->tm_mday, timep->tm_hour, timep->tm_min, + timep->tm_sec, tz); +#endif #else return mktime(timep); #endif From 59100a7251fa358c43cded44a1bf054ffc485e02 Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 7 Aug 2023 17:52:43 +0800 Subject: [PATCH 3/4] enh: code optimize for mktime --- source/os/src/osTime.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index 0430dd70fc..e758055529 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -399,13 +399,12 @@ int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t da // int64_t _res = (((((int64_t) (_year/4 - _year/100 + _year/400 + 367*_mon/12 + day) + // _year*365 - 719499)*24 + hour)*60 + min)*60 + sec); - int64_t _res; - _res = 367 * ((int64_t)_mon) / 12; + int64_t _res = 367 * ((int64_t)_mon) / 12; _res += _year / 4 - _year / 100 + _year / 400 + day + ((int64_t)_year) * 365 - 719499; - _res = _res * 24; + _res *= 24; _res = ((_res + hour) * 60 + min) * 60 + sec; - return (_res + time_zone); + return _res + time_zone; } time_t taosMktime(struct tm *timep) { From b598c2651df65f5c91b5bfad301ce78d3777a096 Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 7 Aug 2023 19:30:54 +0800 Subject: [PATCH 4/4] fix: use mktime after 19700101 on windows --- source/os/src/osTime.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index e758055529..05233065fa 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -447,6 +447,10 @@ time_t taosMktime(struct tm *timep) { t.QuadPart -= offset.QuadPart; return (time_t)(t.QuadPart / 10000000); #else + time_t result = mktime(timep); + if (result != -1) { + return result; + } #ifdef _MSC_VER #if _MSC_VER >= 1900 int64_t tz = _timezone;