-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
91 lines (71 loc) · 2.04 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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)
// if err != nil {
// fmt.Println("error converting ", value, " to int: ", err)
// }
return i
}
// toDecimal converts a string to a decimal value.
func toDecimal(value string) decimal.Decimal {
value = strings.Replace(value, "%", "", -1)
dec, err := decimal.NewFromString(value)
if err != nil {
// fmt.Println("error converting ", value, " to decimal value: ", err)
return decimal.NewFromFloat(0.0)
}
return dec
}
// parseDate converts a string to a proper date.
func parseDate(dString string) time.Time {
d, err := time.Parse("1/2/2006", dString)
if err != nil {
// fmt.Println("error converting ", dString, " to date: ", err)
return time.Time{}
}
return d
}
// parseDateAndTime converts a string to a proper date with a time.
func parseDateAndTime(dString string, tString string) time.Time {
d, err := time.Parse("1/2/2006", dString)
if err != nil {
// fmt.Println("error converting ", dString, " to date: ", err)
return time.Time{}
}
t, err := time.Parse("3:04pm", tString)
if err != nil {
// fmt.Println("error converting ", tString, " to time: ", err)
return time.Time{}
}
loc, _ := time.LoadLocation("America/New_York")
return time.Date(d.Year(), d.Month(), d.Day(), t.Hour(), t.Minute(), t.Second(), 0, loc)
}
// parseDashedDate converts a string to a proper date and sets time to market close.
func parseDashedDate(dString string) time.Time {
date, err := time.Parse("2006-01-02", dString)
if err != nil {
// fmt.Println("error converting ", dString, " to date: ", err)
return time.Time{}
}
return date.Add(time.Hour * 16)
}
func parseMalformedDate(s string) string {
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
}