| 23 | } |
| 24 | |
| 25 | func 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 | |
| 58 | const ( |
| 59 | // ErrCodeTaskInvalidParams is returned when request parameters are invalid. |