Explorar o código

修改时间点比较bug,修改compareWithUpdateTime函数

cuci hai 1 ano
pai
achega
0a7960cf20
Modificáronse 3 ficheiros con 43 adicións e 62 borrados
  1. 41 61
      core/core.c
  2. 1 0
      include/decode.h
  3. 1 1
      lib/decode.c

+ 41 - 61
core/core.c

@@ -1511,93 +1511,73 @@ int compareWithCurrentTime(struct tm t1)
     }
 }
 
-//更新时间和拉合闸的时间点比较
-int compareWithUpdateTime(const char *timeString,const char *time1,const char *time2,const char *time3,const char *time4)
-{
-  struct tm tm_base, tm_timeString, tm_time1, tm_time2,tm_time3, tm_time4;
-
-  // 设置基准日期为当前日期
-  time_t current_time = time(NULL);
-  localtime_r(&current_time, &tm_base);
-
-  // 解析timeString
-  if (strptime(timeString, "%H:%M", &tm_timeString) == 0) 
-  {
-    fprintf(stderr, "Failed to parse tm_timeString\n");
-    return 0; // or handle the error in an appropriate way
-    
-  }
+// 将时间字符串转换为秒数
+int timeStringToSeconds(const char *timeString) {
+    int hours, minutes;
+    if (sscanf(timeString, "%02d:%02d", &hours, &minutes) == 2) {
+        return hours * 3600 + minutes * 60;
+    }
+    return INT_MIN;  // 解析失败
+}
 
-  // 设置日期部分为基准日期的日期部分
-  tm_timeString.tm_year = tm_base.tm_year;
-  tm_timeString.tm_mon = tm_base.tm_mon;
-  tm_timeString.tm_mday = tm_base.tm_mday;
+// 比较两个时间字符串的时间差(秒)
+int compareTimeDifference(const char *timeString1, const char *timeString2) {
+    int seconds1 = timeStringToSeconds(timeString1);
+    int seconds2 = timeStringToSeconds(timeString2);
+    debug("seconds1:%d,seconds2:%d\n",seconds1,seconds2);
 
-  // 将 struct tm 结构转换为 time_t 类型
-  time_t t0 = mktime(&tm_timeString);//重新计算时间
+    if (seconds1 != INT_MIN && seconds2 != INT_MIN) {
+        return seconds1 - seconds2;
+    } else {
+        return INT_MIN;  // 解析失败
+    }
+}
 
+//更新时间和拉合闸的时间点比较
+int compareWithUpdateTime(const char *timeString,const char *time1,const char *time2,const char *time3,const char *time4)
+{
+  int diff1 = compareTimeDifference(timeString, time1);
   // 解析时间字符串1
-  if (strlen(time1)==5 && strptime(time1, "%H:%M", &tm_time1)) 
+  if (strlen(time1)==5 && diff1!= INT_MIN) 
   {
-    tm_time1.tm_year = tm_base.tm_year;
-    tm_time1.tm_mon = tm_base.tm_mon;
-    tm_time1.tm_mday = tm_base.tm_mday;
-    time_t t1 = mktime(&tm_time1);
-    // 计算两个时间的差值
-    double timeDifference = difftime(t0,t1);
-    debug("time1:%s和更新时间:%s,差值为%.2f\n",time1,timeString,timeDifference);
-    if(fabs(timeDifference)<120.0)//更新时间在2分钟内
+    debug("time1:%s,和更新时间:%s,差值为%d\n",time1 ,timeString,diff1);
+    if(fabs(diff1)<120)//更新时间在2分钟内
     {
       debug("time1更新时间在2分钟内\n");
       return 1;
     }
   }
 
+  int diff2 = compareTimeDifference(timeString, time2);
   // 解析时间字符串2
-  if (strlen(time2)==5 && strptime(time2, "%H:%M", &tm_time2)) 
+  if (strlen(time2)==5 && diff2!= INT_MIN) 
   {
-    tm_time2.tm_year = tm_base.tm_year;
-    tm_time2.tm_mon = tm_base.tm_mon;
-    tm_time2.tm_mday = tm_base.tm_mday;
-    time_t t2 = mktime(&tm_time2);
-    // 计算两个时间的差值
-    double timeDifference = difftime(t0,t2);
-    debug("time2:%s和更新时间:%s,差值为%.2f\n",time2,timeString,timeDifference);
-    if(fabs(timeDifference)<120.0)//更新时间在2分钟内
+    debug("time2:%s,和更新时间:%s,差值为%d\n",time2 ,timeString,diff2);
+    if(fabs(diff2)<120)//更新时间在2分钟内
     {
       debug("time2更新时间在2分钟内\n");
       return 1;
     }
   }
 
+  int diff3 = compareTimeDifference(timeString, time3);
   // 解析时间字符串3
-  if (strlen(time3)==5 && strptime(time3, "%H:%M", &tm_time3)) 
-  {
-    tm_time3.tm_year = tm_base.tm_year;
-    tm_time3.tm_mon = tm_base.tm_mon;
-    tm_time3.tm_mday = tm_base.tm_mday;
-    time_t t3 = mktime(&tm_time3);
-    // 计算两个时间的差值
-    double timeDifference = difftime(t0,t3);
-    debug("time3:%s和更新时间:%s,差值为%.2f\n",time3,timeString,timeDifference);
-    if(fabs(timeDifference)<120.0)//更新时间在2分钟内
+  if (strlen(time1)==5 && diff3!= INT_MIN) 
+  {
+    debug("time3:%s,和更新时间:%s,差值为%d\n",time3 ,timeString,diff3);
+    if(fabs(diff3)<120)//更新时间在2分钟内
     {
-      debug("time3更新时间在2分钟内\n");
+      debug("time1更新时间在2分钟内\n");
       return 1;
     }
   }
 
+  int diff4 = compareTimeDifference(timeString, time4);
   // 解析时间字符串4
-  if (strlen(time4)==5 && strptime(time4, "%H:%M", &tm_time4)) 
-  {
-    tm_time4.tm_year = tm_base.tm_year;
-    tm_time4.tm_mon = tm_base.tm_mon;
-    tm_time4.tm_mday = tm_base.tm_mday;
-    time_t t4 = mktime(&tm_time4);
-    // 计算两个时间的差值
-    double timeDifference = difftime(t0,t4);
-    debug("time4:%s和更新时间:%s,差值为%.2f\n",time4,timeString,timeDifference);
-    if(fabs(timeDifference)<120.0)//更新时间在2分钟内
+  if (strlen(time4)==5 && diff4!= INT_MIN) 
+  {
+    debug("time4:%s,和更新时间:%s,差值为%d\n",time4 ,timeString,diff4);
+    if(fabs(diff4)<120)//更新时间在2分钟内
     {
       debug("time4更新时间在2分钟内\n");
       return 1;

+ 1 - 0
include/decode.h

@@ -7,6 +7,7 @@
 #include <time.h>
 #include <errno.h>
 #include <sys/syscall.h>
+#include <limits.h>
 //#include <math.h>
 
 #include "status.h"

+ 1 - 1
lib/decode.c

@@ -56,7 +56,7 @@ void AlarmGateway(void)
 		{
 			CoreAlarmGateway();
 			lastHeartTime = nowTime;
-			log("AlarmGateway used:%ds=(%d-%d)\n", timeGloble_g - nowTime, timeGloble_g, nowTime);
+			log("AlarmGateway used:%ds=(%d-%d)-----------------------------------------------------------------------\n", timeGloble_g - nowTime, timeGloble_g, nowTime);
 			fflush(stdout);
 		}
 	}