This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
/
time.go
79 lines (67 loc) · 1.61 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package util
import (
"fmt"
"strings"
"time"
)
func ParseTimeZ(s string) (time.Time, error) {
t, err := time.Parse(time.RFC3339, s)
return t, err
}
func ParseLocalTime(ts string, loc *time.Location) (time.Time, error) {
t, err := time.ParseInLocation(time.RFC3339, ts, loc)
if e, ok := err.(*time.ParseError); ok && e.LayoutElem == "Z07:00" && e.ValueElem == "" {
t, err = time.ParseInLocation("2006-01-02T15:04:05", ts, loc)
}
return t, err
}
func FormatTimeZ(t time.Time) string {
return t.UTC().Format(time.RFC3339)
}
func FormatLocalTime(t time.Time, loc *time.Location) string {
return t.In(loc).Format(time.RFC3339)
}
func FromMillis(t int64) time.Time {
return time.Unix(t/1000, t%1000*1000000)
}
func LimitToPeriod(p, t [2]time.Time) [2]time.Time {
if t[0].Before(p[0]) || t[0].IsZero() {
t[0] = p[0]
}
if t[1].After(p[1]) || t[1].IsZero() {
t[1] = p[1]
}
return t
}
func ParsePeriod(s string, loc *time.Location) ([2]time.Time, error) {
var r [2]time.Time
if s == "" {
// default: from Unix epoch to now
r[0] = time.Unix(0, 0).UTC()
r[1] = time.Now().UTC()
return r, nil
}
ts := strings.Split(s, "/")
var err error
switch len(ts) {
case 1:
if r[0], err = ParseLocalTime(s, loc); err != nil {
return r, err
}
r[1] = time.Now().In(loc)
return r, nil
case 2:
if r[0], err = ParseLocalTime(ts[0], loc); err != nil {
return r, err
}
if strings.HasPrefix(ts[1], "P") {
// TODO parse duration
} else {
if r[1], err = ParseLocalTime(ts[1], loc); err != nil {
return r, err
}
return r, nil
}
}
return r, fmt.Errorf("Error parsing time interval '%s'", s)
}