MCPcopy Create free account
hub / github.com/OpenListTeam/OpenList / MustParseTime

Function MustParseTime

drivers/lanzou/help.go:21–46  ·  view source on GitHub ↗

如果解析失败,则返回当前时间

(str string)

Source from the content-addressed store, hash-verified

19
20// 如果解析失败,则返回当前时间
21func MustParseTime(str string) time.Time {
22 lastOpTime, err := time.ParseInLocation("2006-01-02 -07", str+" +08", time.Local)
23 if err != nil {
24 strs := timeSplitReg.FindStringSubmatch(str)
25 lastOpTime = time.Now()
26 if len(strs) == 3 {
27 i, _ := strconv.ParseInt(strs[1], 10, 64)
28 ti := time.Duration(-i)
29 switch strs[2] {
30 case "秒前":
31 lastOpTime = lastOpTime.Add(time.Second * ti)
32 case "分钟前":
33 lastOpTime = lastOpTime.Add(time.Minute * ti)
34 case "小时前":
35 lastOpTime = lastOpTime.Add(time.Hour * ti)
36 case "天前":
37 lastOpTime = lastOpTime.Add(DAY * ti)
38 case "昨天":
39 lastOpTime = lastOpTime.Add(-DAY)
40 case "前天":
41 lastOpTime = lastOpTime.Add(-DAY * 2)
42 }
43 }
44 }
45 return lastOpTime
46}
47
48// 解析大小
49var sizeSplitReg = regexp.MustCompile(`(?i)([0-9.]+)\s*([bkm]+)`)

Callers 2

ModTimeMethod · 0.70
ModTimeMethod · 0.70

Calls 2

NowMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected