/
timespec.go
127 lines (108 loc) · 3.79 KB
/
timespec.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package graphite
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/m3db/m3/src/query/graphite/errors"
)
var reRelativeTime = regexp.MustCompile(`(?i)^\-([0-9]+)(s|min|h|d|w|mon|y)$`)
var periods = map[string]time.Duration{
"s": time.Second,
"min": time.Minute,
"h": time.Hour,
"d": time.Hour * 24,
"w": time.Hour * 24 * 7,
"mon": time.Hour * 24 * 30,
"y": time.Hour * 24 * 365,
}
// on Jan 2 15:04:05 -0700 MST 2006
var formats = []string{
"15:04_060102",
"15:04_20060102",
"15:04_01/02/06",
"15:04_02.01.06",
"02.01.06",
"01/02/06",
"060102",
"20060102",
}
// use init to rewrite formats to bypass bug in time.Parse
func init() {
for i := range formats {
formats[i] = bypassTimeParseBug(formats[i])
}
}
func bypassTimeParseBug(s string) string {
// NB(jayp): Looks like there is a bug in Golang's time.Parse when handing format strings
// with _2 in the format string. Here is a snippet that exhibits this issue:
// t, e := time.Parse("15:04_20060102", "14:38_20150618")
// We replace underscores with space to bypass this bug.
return strings.Replace(s, "_", " ", -1)
}
// FormatTime translates a time.Time until a Graphite from/until string
func FormatTime(t time.Time) string {
return t.Format(formats[0])
}
// ParseTime translates a Graphite from/until string into a timestamp relative to the provide time
func ParseTime(s string, now time.Time, absoluteOffset time.Duration) (time.Time, error) {
if len(s) == 0 {
return now, errors.NewInvalidParamsError(fmt.Errorf("time cannot be empty"))
}
if s == "now" {
return now, nil
}
if m := reRelativeTime.FindStringSubmatch(s); len(m) != 0 {
timePast, err := strconv.ParseInt(m[1], 10, 32)
if err != nil {
return now, errors.NewInvalidParamsError(fmt.Errorf("invalid relative time %v", err))
}
period := periods[strings.ToLower(m[2])]
return now.Add(-1 * time.Duration(timePast) * period), nil
}
newS := bypassTimeParseBug(s)
for _, format := range formats {
t, err := time.Parse(format, newS)
if err == nil {
// Absolute time passed in, applying offset
return t.Add(absoluteOffset), nil
}
}
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return time.Unix(n, 0).UTC(), nil
}
return now, err
}
// ParseDuration parses a duration
func ParseDuration(s string) (time.Duration, error) {
if m := reRelativeTime.FindStringSubmatch(s); len(m) != 0 {
timePast, err := strconv.ParseInt(m[1], 10, 32)
if err != nil {
return 0, errors.NewInvalidParamsError(fmt.Errorf("invalid relative time %v", err))
}
period := periods[strings.ToLower(m[2])]
return -1 * time.Duration(timePast) * period, nil
}
return 0, errors.NewInvalidParamsError(fmt.Errorf("invalid relative time %s", s))
}