Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
912df2e2
Unverified
Commit
912df2e2
authored
Jun 10, 2019
by
Carl Bergquist
Committed by
GitHub
Jun 10, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gtime: some code style refactoring (#17369)
parent
bc7c420e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
10 deletions
+15
-10
pkg/components/gtime/gtime.go
+13
-10
pkg/components/gtime/gtime_test.go
+2
-0
No files found.
pkg/components/gtime/gtime.go
View file @
912df2e2
...
...
@@ -6,23 +6,26 @@ import (
"time"
)
// ParseInterval parses and interval with support for all units that Grafana uses.
var
dateUnitPattern
=
regexp
.
MustCompile
(
`(\d+)([wdy])`
)
// ParseInterval parses an interval with support for all units that Grafana uses.
func
ParseInterval
(
interval
string
)
(
time
.
Duration
,
error
)
{
re
:=
regexp
.
MustCompile
(
`(\d+)([wdy])`
)
result
:=
re
.
FindSubmatch
([]
byte
(
interval
))
result
:=
dateUnitPattern
.
FindSubmatch
([]
byte
(
interval
))
if
len
(
result
)
!=
3
{
return
time
.
ParseDuration
(
interval
)
}
if
len
(
result
)
==
3
{
num
,
_
:=
strconv
.
Atoi
(
string
(
result
[
1
]))
period
:=
string
(
result
[
2
])
if
period
==
`d`
{
return
time
.
Hour
*
24
*
time
.
Duration
(
num
),
nil
}
else
if
period
==
`w`
{
return
time
.
Hour
*
24
*
7
*
time
.
Duration
(
num
),
nil
}
else
{
return
time
.
Hour
*
24
*
7
*
365
*
time
.
Duration
(
num
),
nil
}
}
else
{
return
time
.
ParseDuration
(
interval
)
if
period
==
`w`
{
return
time
.
Hour
*
24
*
7
*
time
.
Duration
(
num
),
nil
}
return
time
.
Hour
*
24
*
7
*
365
*
time
.
Duration
(
num
),
nil
}
pkg/components/gtime/gtime_test.go
View file @
912df2e2
...
...
@@ -15,7 +15,9 @@ func TestParseInterval(t *testing.T) {
}{
{
interval
:
"1d"
,
duration
:
time
.
Hour
*
24
},
{
interval
:
"1w"
,
duration
:
time
.
Hour
*
24
*
7
},
{
interval
:
"2w"
,
duration
:
time
.
Hour
*
24
*
7
*
2
},
{
interval
:
"1y"
,
duration
:
time
.
Hour
*
24
*
7
*
365
},
{
interval
:
"5y"
,
duration
:
time
.
Hour
*
24
*
7
*
365
*
5
},
{
interval
:
"1M"
,
err
:
errors
.
New
(
"time: unknown unit M in duration 1M"
)},
{
interval
:
"invalid-duration"
,
err
:
errors
.
New
(
"time: invalid duration invalid-duration"
)},
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment