From 3654b8eb66647171ced687f9670091d686735a53 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Fri, 25 Aug 2023 11:33:27 +0000 Subject: [PATCH 1/9] =?UTF-8?q?update=20modules/alarm/alarm.cpp.=20?= =?UTF-8?q?=E7=94=A8=5Fget=5Ftimezone=20=E5=87=BD=E6=95=B0=E9=87=8D?= =?UTF-8?q?=E5=86=99=20GetSystemTimezoneOffsetSeconds()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index 1f634cb..62b2382 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -106,18 +106,11 @@ uint32_t Alarm::remainSeconds() const { namespace { //! 获取系统的时区偏移秒数 int GetSystemTimezoneOffsetSeconds() { - //! 假设当前0时区的时间是 1970-1-1 12:00,即 utc_ts = 12 * 3600 - //! 通过 localtime_r() 获取本地的时间 local_tm。 - //! 通过 local_tm 中的 hour, min, sec 可计算出本地的时间戳 local_ts。 - //! 再用本地的时间戳减去 utc_ts 即可得出期望的值。 - // - //! 为什么选用0时区的12时,而不是其它时间点呢? - //! 因为在这个时间点上,计算出的任何一个时间的时间都是在 00:00 ~ 23:59 之间的 - struct tm local_tm; - time_t utc_ts = 12 * 3600; - localtime_r(&utc_ts, &local_tm); - int local_ts = local_tm.tm_hour * 3600 + local_tm.tm_min * 60 + local_tm.tm_sec; - return (local_ts - static_cast(utc_ts)); + long tm_gmtoff{}; + errno_t errn = _get_timezone(&tm_gmtoff); + if (errn) + return 0; + return static_cast(tm_gmtoff); } } -- Gitee From 9db8ad9751b02d64e8761243e1ef404020115528 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Fri, 25 Aug 2023 12:23:46 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmingw=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index 62b2382..814aff6 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -106,10 +106,18 @@ uint32_t Alarm::remainSeconds() const { namespace { //! 获取系统的时区偏移秒数 int GetSystemTimezoneOffsetSeconds() { - long tm_gmtoff{}; - errno_t errn = _get_timezone(&tm_gmtoff); - if (errn) - return 0; + long tm_gmtoff{}; +#if (defined(_MSC_VER) || defined(_UCRT)) && !defined(__BIONIC__) + { + errno_t errn = _get_timezone(&tm_gmtoff); + if (errn) + return 0; + } +#elif defined(_WIN32) && !defined(__BIONIC__) && !defined(__WINE__) && !defined(__CYGWIN__) + tm_gmtoff = _timezone; +#else + tm_gmtoff = timezone; +#endif return static_cast(tm_gmtoff); } } -- Gitee From 308992b9cfa3c6a54180aea06fc8531eabd99bcd Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Fri, 25 Aug 2023 12:28:00 +0000 Subject: [PATCH 3/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index 814aff6..d1715c4 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -106,6 +106,9 @@ uint32_t Alarm::remainSeconds() const { namespace { //! 获取系统的时区偏移秒数 int GetSystemTimezoneOffsetSeconds() { +#if (defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>)) + return 0; +#else long tm_gmtoff{}; #if (defined(_MSC_VER) || defined(_UCRT)) && !defined(__BIONIC__) { @@ -119,6 +122,7 @@ int GetSystemTimezoneOffsetSeconds() { tm_gmtoff = timezone; #endif return static_cast(tm_gmtoff); +#endif } } -- Gitee From b845a35b782c6a73e03f72820c5545adcb0b5812 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Fri, 25 Aug 2023 12:30:44 +0000 Subject: [PATCH 4/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index d1715c4..72d4572 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -109,7 +109,7 @@ int GetSystemTimezoneOffsetSeconds() { #if (defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>)) return 0; #else - long tm_gmtoff{}; + long tm_gmtoff = 0; #if (defined(_MSC_VER) || defined(_UCRT)) && !defined(__BIONIC__) { errno_t errn = _get_timezone(&tm_gmtoff); -- Gitee From ac0bd56ddba56b6417dc7e30b4fa9bbe45eedc1a Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Sat, 26 Aug 2023 04:18:12 +0000 Subject: [PATCH 5/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index 72d4572..481d404 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -106,6 +106,7 @@ uint32_t Alarm::remainSeconds() const { namespace { //! 获取系统的时区偏移秒数 int GetSystemTimezoneOffsetSeconds() { +#if defined(__MINGW32__) || defined(_MSC_VER) || defined(_WIN32) #if (defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>)) return 0; #else @@ -123,6 +124,20 @@ int GetSystemTimezoneOffsetSeconds() { #endif return static_cast(tm_gmtoff); #endif +#else + //! 假设当前0时区的时间是 1970-1-1 12:00,即 utc_ts = 12 * 3600 + //! 通过 localtime_r() 获取本地的时间 local_tm。 + //! 通过 local_tm 中的 hour, min, sec 可计算出本地的时间戳 local_ts。 + //! 再用本地的时间戳减去 utc_ts 即可得出期望的值。 + // + //! 为什么选用0时区的12时,而不是其它时间点呢? + //! 因为在这个时间点上,计算出的任何一个时间的时间都是在 00:00 ~ 23:59 之间的 + struct tm local_tm; + time_t utc_ts = 12 * 3600; + localtime_r(&utc_ts, &local_tm); + int local_ts = local_tm.tm_hour * 3600 + local_tm.tm_min * 60 + local_tm.tm_sec; + return (local_ts - static_cast(utc_ts)); +#endif } } -- Gitee From 3a82294af8271044abe621c40e5730fa6bf2eaba Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Mon, 28 Aug 2023 14:32:17 +0000 Subject: [PATCH 6/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index 481d404..a6efef7 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -108,21 +108,21 @@ namespace { int GetSystemTimezoneOffsetSeconds() { #if defined(__MINGW32__) || defined(_MSC_VER) || defined(_WIN32) #if (defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>)) - return 0; + return 0; #else - long tm_gmtoff = 0; + long tm_gmtoff = 0; #if (defined(_MSC_VER) || defined(_UCRT)) && !defined(__BIONIC__) - { - errno_t errn = _get_timezone(&tm_gmtoff); - if (errn) - return 0; - } + { + errno_t errn = _get_timezone(&tm_gmtoff); + if (errn) + return 0; + } #elif defined(_WIN32) && !defined(__BIONIC__) && !defined(__WINE__) && !defined(__CYGWIN__) - tm_gmtoff = _timezone; + tm_gmtoff = _timezone; #else - tm_gmtoff = timezone; + tm_gmtoff = timezone; #endif - return static_cast(tm_gmtoff); + return static_cast(tm_gmtoff); #endif #else //! 假设当前0时区的时间是 1970-1-1 12:00,即 utc_ts = 12 * 3600 -- Gitee From af0763bf6c63e79ebc645e6e4d88ac0cd1358c01 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Tue, 29 Aug 2023 03:59:46 +0000 Subject: [PATCH 7/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index a6efef7..af7deed 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -107,15 +107,17 @@ namespace { //! 获取系统的时区偏移秒数 int GetSystemTimezoneOffsetSeconds() { #if defined(__MINGW32__) || defined(_MSC_VER) || defined(_WIN32) -#if (defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>)) +#if defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>) return 0; #else long tm_gmtoff = 0; #if (defined(_MSC_VER) || defined(_UCRT)) && !defined(__BIONIC__) { errno_t errn = _get_timezone(&tm_gmtoff); - if (errn) + if (errn != 0) { + LogErr("_get_timezone() error:%d", errn); return 0; + } } #elif defined(_WIN32) && !defined(__BIONIC__) && !defined(__WINE__) && !defined(__CYGWIN__) tm_gmtoff = _timezone; -- Gitee From 311cb122aa58375a7adaa25048b3017caee4e733 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Tue, 29 Aug 2023 04:00:43 +0000 Subject: [PATCH 8/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index af7deed..c119d18 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -115,6 +115,7 @@ int GetSystemTimezoneOffsetSeconds() { { errno_t errn = _get_timezone(&tm_gmtoff); if (errn != 0) { + LogErr("can't get timezone offset, not support."); LogErr("_get_timezone() error:%d", errn); return 0; } -- Gitee From d188f4c70502c5df6a3f303f307b54ffbe7dae39 Mon Sep 17 00:00:00 2001 From: MacroModel <9573434+MacroModel@user.noreply.gitee.com> Date: Tue, 29 Aug 2023 04:05:23 +0000 Subject: [PATCH 9/9] update modules/alarm/alarm.cpp. Signed-off-by: MacroModel <9573434+MacroModel@user.noreply.gitee.com> --- modules/alarm/alarm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/alarm/alarm.cpp b/modules/alarm/alarm.cpp index c119d18..cb77753 100644 --- a/modules/alarm/alarm.cpp +++ b/modules/alarm/alarm.cpp @@ -108,6 +108,7 @@ namespace { int GetSystemTimezoneOffsetSeconds() { #if defined(__MINGW32__) || defined(_MSC_VER) || defined(_WIN32) #if defined(__MINGW32__) && !__has_include(<_mingw_stat64.h>) + LogErr("can't get timezone offset, not support."); return 0; #else long tm_gmtoff = 0; @@ -115,7 +116,6 @@ int GetSystemTimezoneOffsetSeconds() { { errno_t errn = _get_timezone(&tm_gmtoff); if (errn != 0) { - LogErr("can't get timezone offset, not support."); LogErr("_get_timezone() error:%d", errn); return 0; } -- Gitee