-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
47 lines (42 loc) · 1.06 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package time
import (
"log"
"regexp"
"strconv"
"time"
"github.com/pkg/errors"
)
var durationRegex = regexp.MustCompile(`^(\d+)([smhd])$`)
// ParseDuration parses a duration string and returns the time.Duration
func ParseDuration(duration string) (*time.Duration, error) {
matches := durationRegex.FindStringSubmatch(duration)
if len(matches) != 3 {
return nil, errors.Errorf("Invalid since format '%s'. Expected format <duration><unit> (e.g. 3h)\n", duration)
}
amount, err := strconv.ParseInt(matches[1], 10, 64)
if err != nil {
log.Fatal(err)
}
var unit time.Duration
switch matches[2] {
case "s":
unit = time.Second
case "m":
unit = time.Minute
case "h":
unit = time.Hour
case "d":
unit = time.Hour * 24
}
dur := unit * time.Duration(amount)
return &dur, nil
}
// ParseSince parses a duration string and returns a time.Time in history relative to current time
func ParseSince(duration string) (*time.Time, error) {
dur, err := ParseDuration(duration)
if err != nil {
return nil, err
}
since := time.Now().UTC().Add(-*dur)
return &since, nil
}