From a7fb25b2f715fafc6dd462aaeed0747e46d62304 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 25 Nov 2020 10:16:09 +0800 Subject: [PATCH] 1 --- a8/sysutils.cc | 37 +++++++++++++++++++++++++++++++++++++ a8/sysutils.h | 2 ++ a8/timer.cc | 10 ++++++++++ a8/timer.h | 4 ++++ 4 files changed, 53 insertions(+) diff --git a/a8/sysutils.cc b/a8/sysutils.cc index 1a365a3..038a4be 100644 --- a/a8/sysutils.cc +++ b/a8/sysutils.cc @@ -209,6 +209,32 @@ namespace a8 return time_t((time_val + g_time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * g_time_zone; } + time_t GetMondaySeconds(time_t time_val) + { + struct tm tm_time = {0}; + struct tm *ptr = localtime_r(&time_val, &tm_time); + if (!ptr) { + abort(); + } + + switch (tm_time.tm_wday) { + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + return GetDaySeconds(time_val - (3600 * 24 * (tm_time.tm_wday - 1))); + break; + case 0: + return GetDaySeconds(time_val - (3600 * 24 * 6)); + break; + default: + abort(); + return time_val; + } + } + time_t BetweenDays(time_t time_val1, time_t time_val2) { int g_time_zone = 8; @@ -230,6 +256,17 @@ namespace a8 #endif } + time_t GetTimeDaySeconds(const char* time_str) + { + struct tm stm = {0}; + char* p = strptime(time_str, "%Y-%m-%d %H:%M:%S", &stm); + if (!p) { + return -1; + } + long t = mktime(&stm); + return GetDaySeconds(t); + } + long long MakeInt64(int low32, int high32) { return low32 + ((long long)high32 << 32); diff --git a/a8/sysutils.h b/a8/sysutils.h index 5b00577..cb6cf7a 100644 --- a/a8/sysutils.h +++ b/a8/sysutils.h @@ -80,8 +80,10 @@ namespace a8 std::string TimestampToDateTime(time_t time_val); std::string TimestampToDate(time_t time_val); time_t GetDaySeconds(time_t time_val, int incdays = 0); + time_t GetMondaySeconds(time_t time_val); time_t BetweenDays(time_t time_val1, time_t time_val2); time_t GetDateTimeSeconds(const char* datetime_str); + time_t GetTimeDaySeconds(const char* time_str); long long MakeInt64(int low32, int high32); int Low32(long long int64_val); diff --git a/a8/timer.cc b/a8/timer.cc index 98a81ec..f2ff875 100644 --- a/a8/timer.cc +++ b/a8/timer.cc @@ -213,6 +213,16 @@ namespace a8 return timer; } + timer_list* Timer::AddFixedTimerAndAttach(int milli_seconds, + a8::XParams param, + a8::TimerFunc timer_func, + list_head* attach_list) + { + timer_list *timer = AddFixedTimer(milli_seconds, param, timer_func); + list_add_tail(&timer->attach_entry, attach_list); + return timer; + } + void Timer::ModifyTimer(timer_list* timer, int milli_seconds) { DetachTimer(timer); diff --git a/a8/timer.h b/a8/timer.h index e9b3818..7aba687 100644 --- a/a8/timer.h +++ b/a8/timer.h @@ -39,6 +39,10 @@ namespace a8 a8::TimerAfterFunc timer_after_func = nullptr); //添加固定时间定时器(每天,每次重启一天里可能会多次调用) timer_list* AddFixedTimer(int milli_seconds, a8::XParams param, a8::TimerFunc timer_func); + timer_list* AddFixedTimerAndAttach(int milli_seconds, + a8::XParams param, + a8::TimerFunc timer_func, + list_head* attach_list); //修改定时器参数 void ModifyTimer(timer_list* timer, int milli_seconds); //删除定时器