Browse Source

feat: 采集时间解析考虑时区

HeWang 5 months ago
parent
commit
1b1c643b7a

+ 6 - 4
aarch64/pjibot_delivery/common/config/c_cloud.go

@@ -180,17 +180,19 @@ func InitCloudConfig() {
 		c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
 		os.Exit(-1)
 	}
+	cw := time.Now()
+	loc := cw.Location()
 	// 单独解析采集时间
-	startTime, err := time.Parse("15:04", newCloudConfig.CollectWindow.Start)
+	startTime, err := time.ParseInLocation("15:04", newCloudConfig.CollectWindow.Start, loc)
 	if err != nil {
 		c_log.GlobalLogger.Error("云端配置文件解析采集时间【startTime】失败 ", err, "取默认值【00:00】")
-		newCloudConfig.CollectWindow.StartTime, _ = time.Parse("15:04", "00:00")
+		newCloudConfig.CollectWindow.StartTime, _ = time.ParseInLocation("15:04", "00:00", loc)
 	}
 	newCloudConfig.CollectWindow.StartTime = startTime
-	endTime, err := time.Parse("15:04", newCloudConfig.CollectWindow.End)
+	endTime, err := time.ParseInLocation("15:04", newCloudConfig.CollectWindow.End, loc)
 	if err != nil {
 		c_log.GlobalLogger.Error("云端配置文件解析采集时间【endTime】失败 ", err, "取默认值【23:59】")
-		newCloudConfig.CollectWindow.EndTime, _ = time.Parse("15:04", "23:59")
+		newCloudConfig.CollectWindow.EndTime, _ = time.ParseInLocation("15:04", "23:59", loc)
 	}
 	newCloudConfig.CollectWindow.EndTime = endTime
 	if len(newCloudConfig.CollectWindow.Days) == 0 {

+ 3 - 2
aarch64/pjibot_delivery/control/main.go

@@ -52,6 +52,7 @@ func init() {
 }
 
 func isTimeAllowed(currentTime time.Time) bool {
+	loc := currentTime.Location()
 	cw := commonConfig.CloudConfig.CollectWindow
 	if cw.Flag == 0 { // 关闭固定时间段采集, 则都返回true
 		return true
@@ -70,8 +71,8 @@ func isTimeAllowed(currentTime time.Time) bool {
 		}
 	}
 
-	start := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.StartTime.Hour(), cw.StartTime.Minute(), cw.StartTime.Second(), 0, currentTime.Location())
-	end := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.EndTime.Hour(), cw.EndTime.Minute(), cw.EndTime.Second(), 0, currentTime.Location())
+	start := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.StartTime.Hour(), cw.StartTime.Minute(), cw.StartTime.Second(), 0, loc)
+	end := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.EndTime.Hour(), cw.EndTime.Minute(), cw.EndTime.Second(), 0, loc)
 
 	if start.After(end) { // 如果时段跨天
 		end = end.AddDate(0, 0, 1)

+ 8 - 6
tools/pji/pji_time/main/main.go

@@ -24,17 +24,19 @@ func IsTimeAllowed(currentTime time.Time, startStr, endStr string) bool {
 		Start: startStr,
 		End:   endStr,
 	}
+
+	loc := currentTime.Location()
 	// 单独解析采集时间
-	startTime, err := time.Parse("15:04", cw.Start)
+	startTime, err := time.ParseInLocation("15:04", cw.Start, loc)
 	if err != nil {
 		fmt.Println("云端配置文件解析采集时间【startTime】失败 ", err, "取默认值【00:00】")
-		cw.StartTime, _ = time.Parse("15:04", "00:00")
+		cw.StartTime, _ = time.ParseInLocation("15:04", "00:00", loc)
 	}
 	cw.StartTime = startTime
-	endTime, err := time.Parse("15:04", cw.End)
+	endTime, err := time.ParseInLocation("15:04", cw.End, loc)
 	if err != nil {
 		fmt.Println("云端配置文件解析采集时间【endTime】失败 ", err, "取默认值【23:59】")
-		cw.EndTime, _ = time.Parse("15:04", "23:59")
+		cw.EndTime, _ = time.ParseInLocation("15:04", "23:59", loc)
 	}
 	cw.EndTime = endTime
 	if len(cw.Days) == 0 {
@@ -64,8 +66,8 @@ func IsTimeAllowed(currentTime time.Time, startStr, endStr string) bool {
 		}
 	}
 
-	start := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.StartTime.Hour(), cw.StartTime.Minute(), cw.StartTime.Second(), 0, currentTime.Location())
-	end := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.EndTime.Hour(), cw.EndTime.Minute(), cw.EndTime.Second(), 0, currentTime.Location())
+	start := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.StartTime.Hour(), cw.StartTime.Minute(), cw.StartTime.Second(), 0, loc)
+	end := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), cw.EndTime.Hour(), cw.EndTime.Minute(), cw.EndTime.Second(), 0, loc)
 
 	fmt.Println("start", start)
 	fmt.Println("end", end)