forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
40 lines (37 loc) · 908 Bytes
/
parse.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
package chart
import (
"strconv"
"strings"
"time"
)
// ParseFloats parses a list of floats.
func ParseFloats(values ...string) ([]float64, error) {
var output []float64
var parsedValue float64
var err error
var cleaned string
for _, value := range values {
cleaned = strings.TrimSpace(strings.Replace(value, ",", "", -1))
if cleaned == "" {
continue
}
if parsedValue, err = strconv.ParseFloat(cleaned, 64); err != nil {
return nil, err
}
output = append(output, parsedValue)
}
return output, nil
}
// ParseTimes parses a list of times with a given format.
func ParseTimes(layout string, values ...string) ([]time.Time, error) {
var output []time.Time
var parsedValue time.Time
var err error
for _, value := range values {
if parsedValue, err = time.Parse(layout, value); err != nil {
return nil, err
}
output = append(output, parsedValue)
}
return output, nil
}