瀏覽代碼

兼容各种日期格式

hsun 7 月之前
父節點
當前提交
d3bc82466c
共有 2 個文件被更改,包括 82 次插入0 次删除
  1. 52 0
      utils/common.go
  2. 30 0
      watch/watch.go

+ 52 - 0
utils/common.go

@@ -1118,3 +1118,55 @@ func GetTimeSubDay(t1, t2 time.Time) int {
 
 	return day
 }
+
+// AppendMonthDayFromDiffDate 日期补充完整
+func AppendMonthDayFromDiffDate(strDate string) (newDate string, err error) {
+	// 如果能直接解析成完整年月日的格式, 那么直接返回
+	tryDate := []string{"2006-01-02", "2006/01/02", "2006-1-2", "2006/1/2"}
+	for _, v := range tryDate {
+		t, e := time.ParseInLocation(v, strDate, time.Local)
+		if e == nil {
+			newDate = t.Format("2006-01-02")
+			return
+		}
+	}
+
+	// 尝试解析月份格式-补充日
+	tryDate = []string{"2006-01", "2006/01", "2006-1", "2006/1", "2006年1月"}
+	var date time.Time
+	for _, v := range tryDate {
+		d, e := time.ParseInLocation(v, strDate, time.Local)
+		if e == nil {
+			date = d
+			break
+		}
+	}
+	if !date.IsZero() {
+		strMonth := fmt.Sprintf("%d", date.Month())
+		if date.Month() < 10 {
+			strMonth = fmt.Sprintf("0%s", strMonth)
+		}
+		m := fmt.Sprintf("%d-%s-01 00:00:00", date.Year(), strMonth)
+		preTime, e := time.ParseInLocation("2006-01-02 15:04:05", m, time.Local)
+		if e != nil {
+			return "", e
+		}
+		newDate = time.Date(date.Year(), preTime.Month()+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
+		return
+	}
+
+	// 尝试解析年份格式-补充月日
+	tryDate = []string{"2006", "2006年"}
+	for _, v := range tryDate {
+		d, e := time.ParseInLocation(v, strDate, time.Local)
+		if e == nil {
+			date = d
+			break
+		}
+	}
+	if date.IsZero() {
+		return "", fmt.Errorf("时间格式错误, str: %s", strDate)
+	}
+	newDate = time.Date(date.Year(), 12, 31, 0, 0, 0, 0, time.Local).Format("2006-01-02")
+	return
+}

+ 30 - 0
watch/watch.go

@@ -210,8 +210,28 @@ func HandleIndexFile(filePath string) {
 					case 6: //时间区间
 						dateArr := strings.Split(colCell, "~")
 						if len(dateArr) >= 2 {
+							// 兼容不同的日期格式
 							startDateMap[k-1] = dateArr[0]
+							_, e := time.ParseInLocation(utils.FormatDate, dateArr[0], time.Local)
+							if e != nil {
+								st, e := utils.AppendMonthDayFromDiffDate(dateArr[0])
+								if e != nil {
+									fmt.Printf("AppendEndDayFromStrMonthDate1 strDate: %s, Err:%v\n", colCell, e)
+									return
+								}
+								startDateMap[k-1] = st
+							}
+
 							endDateMap[k-1] = dateArr[1]
+							_, e = time.ParseInLocation(utils.FormatDate, dateArr[1], time.Local)
+							if e != nil {
+								ed, e := utils.AppendMonthDayFromDiffDate(dateArr[1])
+								if e != nil {
+									fmt.Printf("AppendEndDayFromStrMonthDate2 strDate: %s, Err:%v\n", colCell, e)
+									return
+								}
+								endDateMap[k-1] = ed
+							}
 						}
 					case 7: //备注
 						describeMap[k-1] = colCell
@@ -224,6 +244,16 @@ func HandleIndexFile(filePath string) {
 				for k, col := range cols {
 					if k == 0 {
 						date = col
+						// 兼容不同的日期格式
+						_, e := time.ParseInLocation(utils.FormatDate, date, time.Local)
+						if e != nil {
+							d, e := utils.AppendMonthDayFromDiffDate(date)
+							if e != nil {
+								fmt.Printf("AppendEndDayFromStrMonthDate3 strDate: %s, Err:%v\n", col, e)
+								return
+							}
+							date = d
+						}
 						continue
 					}
 					if date == `` {