-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_offset.go
49 lines (46 loc) · 1.17 KB
/
date_offset.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
48
49
package watchmaker
import (
"fmt"
"strconv"
"strings"
"time"
)
func CalculateOffset(offsetStr string) (time.Duration, error) {
if offsetStr == "" || offsetStr == "0" || offsetStr == "null" {
return 0, nil
}
// try parsing into a time string
if t, err := ParseDateAny(offsetStr); err == nil {
if t.UTC().Nanosecond() > time.Now().UTC().Nanosecond() {
return time.Now().Sub(t), nil
}
return t.Sub(time.Now()), nil
}
// try parsing into seconds, minutes, hours, days, years
offsetStr = strings.ToLower(offsetStr)
unit := offsetStr[len(offsetStr)-1]
value, err := strconv.Atoi(offsetStr)
if err != nil {
value, err = strconv.Atoi(offsetStr[:len(offsetStr)-1])
if err != nil {
return 0, fmt.Errorf("unable to parse offset:%v", err)
}
}
var res time.Duration
switch unit {
case 's':
res = time.Duration(value) * time.Second
case 'm':
res = time.Duration(value) * time.Minute
case 'h':
res = time.Duration(value) * time.Hour
case 'd':
res = time.Duration(value) * 24 * time.Hour
case 'y':
res = time.Duration(value) * 365 * 24 * time.Hour
default:
// processed by seconds by default
res = time.Duration(value) * time.Second
}
return res, nil
}