|
1 | | -// Copyright 2022 Gitea. All rights reserved. |
| 1 | +// Copyright 2024 Gitea. All rights reserved. |
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | 4 | package util |
5 | 5 |
|
6 | 6 | import ( |
7 | 7 | "fmt" |
8 | | - "math" |
9 | 8 | "regexp" |
10 | 9 | "strconv" |
11 | 10 | "strings" |
| 11 | + "sync" |
12 | 12 | ) |
13 | 13 |
|
14 | | -var ( |
15 | | - // Time estimate match regex |
16 | | - rTimeEstimateOnlyHours = regexp.MustCompile(`^([\d]+)$`) |
17 | | - rTimeEstimateWeeks = regexp.MustCompile(`([\d]+)w`) |
18 | | - rTimeEstimateDays = regexp.MustCompile(`([\d]+)d`) |
19 | | - rTimeEstimateHours = regexp.MustCompile(`([\d]+)h`) |
20 | | - rTimeEstimateMinutes = regexp.MustCompile(`([\d]+)m`) |
21 | | -) |
22 | | - |
23 | | -// TimeEstimateFromStr returns time estimate in seconds from formatted string |
24 | | -func TimeEstimateFromStr(timeStr string) int64 { |
25 | | - timeTotal := 0 |
| 14 | +type timeStrGlobalVarsType struct { |
| 15 | + units []struct { |
| 16 | + name string |
| 17 | + num int64 |
| 18 | + } |
| 19 | + re *regexp.Regexp |
| 20 | +} |
26 | 21 |
|
27 | | - // If single number entered, assume hours |
28 | | - timeStrMatches := rTimeEstimateOnlyHours.FindStringSubmatch(timeStr) |
29 | | - if len(timeStrMatches) > 0 { |
30 | | - raw, _ := strconv.Atoi(timeStrMatches[1]) |
31 | | - timeTotal += raw * (60 * 60) |
32 | | - } else { |
33 | | - // Find time weeks |
34 | | - timeStrMatches = rTimeEstimateWeeks.FindStringSubmatch(timeStr) |
35 | | - if len(timeStrMatches) > 0 { |
36 | | - raw, _ := strconv.Atoi(timeStrMatches[1]) |
37 | | - timeTotal += raw * (60 * 60 * 24 * 7) |
38 | | - } |
| 22 | +var timeStrGlobalVars = sync.OnceValue[*timeStrGlobalVarsType](func() *timeStrGlobalVarsType { |
| 23 | + v := &timeStrGlobalVarsType{} |
| 24 | + v.re = regexp.MustCompile(`(?i)(\d+)\s*([dhms])`) |
| 25 | + v.units = []struct { |
| 26 | + name string |
| 27 | + num int64 |
| 28 | + }{ |
| 29 | + {"d", 60 * 60 * 24}, |
| 30 | + {"h", 60 * 60}, |
| 31 | + {"m", 60}, |
| 32 | + {"s", 1}, |
| 33 | + } |
| 34 | + return v |
| 35 | +}) |
39 | 36 |
|
40 | | - // Find time days |
41 | | - timeStrMatches = rTimeEstimateDays.FindStringSubmatch(timeStr) |
42 | | - if len(timeStrMatches) > 0 { |
43 | | - raw, _ := strconv.Atoi(timeStrMatches[1]) |
44 | | - timeTotal += raw * (60 * 60 * 24) |
| 37 | +func TimeEstimateParse(timeStr string) (int64, error) { |
| 38 | + if timeStr == "" { |
| 39 | + return 0, nil |
| 40 | + } |
| 41 | + var total int64 |
| 42 | + matches := timeStrGlobalVars().re.FindAllStringSubmatchIndex(timeStr, -1) |
| 43 | + if len(matches) == 0 { |
| 44 | + return 0, fmt.Errorf("invalid time string: %s", timeStr) |
| 45 | + } |
| 46 | + if matches[0][0] != 0 || matches[len(matches)-1][1] != len(timeStr) { |
| 47 | + return 0, fmt.Errorf("invalid time string: %s", timeStr) |
| 48 | + } |
| 49 | + for _, match := range matches { |
| 50 | + amount, err := strconv.ParseInt(timeStr[match[2]:match[3]], 10, 64) |
| 51 | + if err != nil { |
| 52 | + return 0, fmt.Errorf("invalid time string: %v", err) |
45 | 53 | } |
46 | | - |
47 | | - // Find time hours |
48 | | - timeStrMatches = rTimeEstimateHours.FindStringSubmatch(timeStr) |
49 | | - if len(timeStrMatches) > 0 { |
50 | | - raw, _ := strconv.Atoi(timeStrMatches[1]) |
51 | | - timeTotal += raw * (60 * 60) |
| 54 | + unit := timeStr[match[4]:match[5]] |
| 55 | + found := false |
| 56 | + for _, u := range timeStrGlobalVars().units { |
| 57 | + if strings.ToLower(unit) == u.name { |
| 58 | + total += amount * u.num |
| 59 | + found = true |
| 60 | + break |
| 61 | + } |
52 | 62 | } |
53 | | - |
54 | | - // Find time minutes |
55 | | - timeStrMatches = rTimeEstimateMinutes.FindStringSubmatch(timeStr) |
56 | | - if len(timeStrMatches) > 0 { |
57 | | - raw, _ := strconv.Atoi(timeStrMatches[1]) |
58 | | - timeTotal += raw * (60) |
| 63 | + if !found { |
| 64 | + return 0, fmt.Errorf("invalid time unit: %s", unit) |
59 | 65 | } |
60 | 66 | } |
61 | | - |
62 | | - return int64(timeTotal) |
| 67 | + return total, nil |
63 | 68 | } |
64 | 69 |
|
65 | | -// TimeEstimateStr returns formatted time estimate string from seconds (e.g. "2w 4d 12h 5m") |
66 | | -func TimeEstimateToStr(amount int64) string { |
| 70 | +func TimeEstimateString(amount int64) string { |
67 | 71 | var timeParts []string |
68 | | - |
69 | | - timeSeconds := float64(amount) |
70 | | - |
71 | | - // Format weeks |
72 | | - weeks := math.Floor(timeSeconds / (60 * 60 * 24 * 7)) |
73 | | - if weeks > 0 { |
74 | | - timeParts = append(timeParts, fmt.Sprintf("%dw", int64(weeks))) |
75 | | - } |
76 | | - timeSeconds -= weeks * (60 * 60 * 24 * 7) |
77 | | - |
78 | | - // Format days |
79 | | - days := math.Floor(timeSeconds / (60 * 60 * 24)) |
80 | | - if days > 0 { |
81 | | - timeParts = append(timeParts, fmt.Sprintf("%dd", int64(days))) |
82 | | - } |
83 | | - timeSeconds -= days * (60 * 60 * 24) |
84 | | - |
85 | | - // Format hours |
86 | | - hours := math.Floor(timeSeconds / (60 * 60)) |
87 | | - if hours > 0 { |
88 | | - timeParts = append(timeParts, fmt.Sprintf("%dh", int64(hours))) |
89 | | - } |
90 | | - timeSeconds -= hours * (60 * 60) |
91 | | - |
92 | | - // Format minutes |
93 | | - minutes := math.Floor(timeSeconds / (60)) |
94 | | - if minutes > 0 { |
95 | | - timeParts = append(timeParts, fmt.Sprintf("%dm", int64(minutes))) |
| 72 | + for _, u := range timeStrGlobalVars().units { |
| 73 | + if amount >= u.num { |
| 74 | + num := amount / u.num |
| 75 | + amount %= u.num |
| 76 | + timeParts = append(timeParts, fmt.Sprintf("%d%s", num, u.name)) |
| 77 | + } |
96 | 78 | } |
97 | | - |
98 | 79 | return strings.Join(timeParts, " ") |
99 | 80 | } |
0 commit comments