MCPcopy Index your code
hub / github.com/larksuite/cli / parseRelativeTime

Function parseRelativeTime

shortcuts/task/task_util.go:25–56  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

23}
24
25func parseRelativeTime(s string) (time.Time, error) {
26 matches := relativeTimeRe.FindStringSubmatch(s)
27 if len(matches) == 0 {
28 return time.Time{}, errs.NewValidationError(errs.SubtypeInvalidArgument, "invalid relative time format: %s", s)
29 }
30
31 sign := matches[1]
32 amountStr := matches[2]
33 unit := matches[3]
34
35 amount, err := strconv.Atoi(amountStr)
36 if err != nil {
37 return time.Time{}, err
38 }
39
40 if sign == "-" {
41 amount = -amount
42 }
43
44 now := time.Now()
45 switch unit {
46 case "d":
47 return now.AddDate(0, 0, amount), nil
48 case "w":
49 return now.AddDate(0, 0, amount*7), nil
50 case "m":
51 return now.Add(time.Duration(amount) * time.Minute), nil
52 case "h":
53 return now.Add(time.Duration(amount) * time.Hour), nil
54 }
55 panic(fmt.Sprintf("unreachable: relativeTimeRe matched unexpected unit %q", unit))
56}
57
58const (
59 // ErrCodeTaskInvalidParams is returned when request parameters are invalid.

Callers 3

parseTaskTimeFunction · 0.85
parseTimeFlagSecFunction · 0.85

Calls 2

NewValidationErrorFunction · 0.92
DurationMethod · 0.80

Tested by 1