-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
36 lines (32 loc) · 975 Bytes
/
utils.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
package timeutils
import (
"strconv"
"strings"
"time"
)
// GetTimestamp tries to parse given string as golang duration,
// then RFC3339 time and finally as a Unix timestamp. If
// any of these were successful, it returns a Unix timestamp
// as string otherwise returns the given value back.
// In case of duration input, the returned timestamp is computed
// as the given reference time minus the amount of the duration.
func GetTimestamp(value string, reference time.Time) string {
if d, err := time.ParseDuration(value); value != "0" && err == nil {
return strconv.FormatInt(reference.Add(-d).Unix(), 10)
}
var format string
if strings.Contains(value, ".") {
format = time.RFC3339Nano
} else {
format = time.RFC3339
}
loc := time.FixedZone(time.Now().Zone())
if len(value) < len(format) {
format = format[:len(value)]
}
t, err := time.ParseInLocation(format, value, loc)
if err != nil {
return value
}
return strconv.FormatInt(t.Unix(), 10)
}