go-chart/parse.go

41 lines
908 B
Go
Raw Normal View History

2019-02-13 19:09:26 -05:00
package chart
import (
"strconv"
2019-02-13 21:55:13 -05:00
"strings"
2019-02-13 19:09:26 -05:00
"time"
)
// ParseFloats parses a list of floats.
func ParseFloats(values ...string) ([]float64, error) {
var output []float64
var parsedValue float64
var err error
2019-02-13 21:55:13 -05:00
var cleaned string
2019-02-13 19:09:26 -05:00
for _, value := range values {
2019-02-13 21:55:13 -05:00
cleaned = strings.TrimSpace(strings.Replace(value, ",", "", -1))
if cleaned == "" {
continue
}
if parsedValue, err = strconv.ParseFloat(cleaned, 64); err != nil {
2019-04-24 15:58:05 -04:00
return nil, err
2019-02-13 19:09:26 -05:00
}
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 {
2019-04-24 15:58:05 -04:00
return nil, err
2019-02-13 19:09:26 -05:00
}
output = append(output, parsedValue)
}
return output, nil
}