-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
112 lines (95 loc) · 2.8 KB
/
util.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
)
func GoogleMapsAPIKey() string {
return ""
}
func FormatUnixTime(unixTime int64, formatStr string) string {
if unixTime == 0 {
return ""
}
if formatStr == "" {
formatStr = "Jan 2 15:04 MST 2006"
}
EasternTZ, _ := time.LoadLocation("America/New_York")
realDate := time.Unix(unixTime, 0).In(EasternTZ)
return realDate.Format(formatStr)
}
func FormatDatetimeStr(dateStr string, formatStr string) string {
if formatStr == "" {
formatStr = "Jan 2"
}
var dateObj time.Time
if len(dateStr) == 10 {
dateObj, _ = time.Parse("2006-01-02", dateStr)
} else if len(dateStr) == 19 {
dateObj, _ = time.Parse("2006-01-02 15:04:05", dateStr)
} else {
log.Fatal().Str("dateStr", dateStr).Msg("Unknown how to parse this datetime string")
}
return dateObj.Format(formatStr)
}
func UnixToDatetimeStr(unixTime int64) string {
dateTime := time.Unix(unixTime, 0)
return dateTime.Format("2006-01-02 15:04:05")
}
func Over24Hours(dateStr string) bool {
var dateObj time.Time
if len(dateStr) == 10 {
dateObj, _ = time.Parse("2006-01-02", dateStr)
} else if len(dateStr) == 19 {
dateObj, _ = time.Parse("2006-01-02 15:04:05", dateStr)
} else {
log.Fatal().Str("dateStr", dateStr).Msg("Unknown how to parse this datetime string")
}
EasternTZ, _ := time.LoadLocation("America/New_York")
currentDate := time.Now().In(EasternTZ)
dur := currentDate.Sub(dateObj)
return dur.Hours() >= 24.0
}
func Over24HoursUTC(dateStr string) bool {
var dateObj time.Time
if len(dateStr) == 10 {
dateObj, _ = time.Parse("2006-01-02", dateStr)
} else if len(dateStr) == 19 {
dateObj, _ = time.Parse("2006-01-02 15:04:05", dateStr)
} else {
log.Fatal().Str("dateStr", dateStr).Msg("Unknown how to parse this datetime string")
}
currentDate := time.Now()
dur := currentDate.Sub(dateObj)
return dur.Hours() >= 24.0
}
func GradeColor(gradeStr string) string {
lcGradeStr := strings.ToLower(gradeStr)
switch lcGradeStr {
case "strong buy":
return "text-success"
case "buy", "outperform", "moderate buy", "accumulate", "overweight", "add", "market perform", "sector perform":
return "text-success"
case "hold", "neutral", "in-line", "equal-weight":
return "text-warning"
case "sell", "underperform", "moderate sell", "weak hold", "underweight", "reduce", "market underperform", "sector underperform":
return "text-danger"
case "strong sell":
return "text-danger"
default:
return "text-white"
}
}
func SinceColor(sinceStr string) string {
lcSinceStr := strings.ToLower(sinceStr)
up_rx := regexp.MustCompile(`^(and|but) up `)
down_rx := regexp.MustCompile(`^(and|but) down `)
if up_rx.MatchString(lcSinceStr) {
return "text-success"
} else if down_rx.MatchString(lcSinceStr) {
return "text-danger"
} else {
return "text-white"
}
}