You've already forked VptPassiveAdapter
Initial
This commit is contained in:
39
util/parse_time.go
Normal file
39
util/parse_time.go
Normal file
@ -0,0 +1,39 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"ZhenTuLocalPassiveAdapter/config"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ParseStartStopTime(filePath string, relativeDate time.Time) (time.Time, time.Time, error) {
|
||||
split := strings.Split(filePath, config.Config.FileName.TimeSplit)
|
||||
if len(split) != 2 {
|
||||
return time.Time{}, time.Time{}, fmt.Errorf("无法解析时间范围")
|
||||
}
|
||||
startTime, err := ParseTime(split[0], relativeDate)
|
||||
if err != nil {
|
||||
return time.Time{}, time.Time{}, err
|
||||
}
|
||||
stopTime, err := ParseTime(split[1], relativeDate)
|
||||
if err != nil {
|
||||
return time.Time{}, time.Time{}, err
|
||||
}
|
||||
return startTime, stopTime, nil
|
||||
}
|
||||
|
||||
func ParseTime(s string, relativeDate time.Time) (time.Time, error) {
|
||||
re := regexp.MustCompile(`(\d{2}).?(\d{2}).?(\d{2})`)
|
||||
matches := re.FindStringSubmatch(s)
|
||||
if len(matches) != 4 {
|
||||
return time.Time{}, fmt.Errorf("无法解析时间范围")
|
||||
}
|
||||
tm, err := time.Parse("15.04.05", fmt.Sprintf("%s.%s.%s", matches[1], matches[2], matches[3]))
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
return time.Date(relativeDate.Year(), relativeDate.Month(), relativeDate.Day(),
|
||||
tm.Hour(), tm.Minute(), tm.Second(), 0, time.Local), nil
|
||||
}
|
Reference in New Issue
Block a user