forked from nivertech/go-finance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
74 lines (59 loc) · 1.26 KB
/
utils.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
package finance
import (
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
)
// toInt converts a string to an int.
func toInt(value string) int {
i, _ := strconv.Atoi(value)
return i
}
// toDecimal converts a string to a decimal value.
func toDecimal(value string) (d decimal.Decimal) {
value = strings.Replace(value, "%", "", -1)
d, _ = decimal.NewFromString(value)
return
}
func toEventValue(value string) Value {
if strings.Contains(value, ":") {
return Value{
Ratio: value,
}
}
return Value{
Dividend: toDecimal(value),
}
}
// parseDashedDate converts a string to a proper date and sets time to market close.
func parseDashedDate(s string) (d time.Time, err error) {
if !strings.ContainsAny(s, "0123456789") {
return
}
d, err = time.Parse("2006-01-02", s)
if err != nil {
s = parseMalformedDate(s)
d, err = time.Parse("2006-01-02", s)
if err != nil {
return time.Time{}, err
}
}
return
}
func parseMalformedDate(s string) string {
if len(s) < 7 {
return s
}
chars := strings.Split(s, "")
chars = chars[1:]
chars = insert(chars, 4, "-")
chars = insert(chars, 7, "-")
return strings.Join(chars[:], "")
}
func insert(s []string, i int, x string) []string {
s = append(s, "")
copy(s[i+1:], s[i:])
s[i] = x
return s
}