-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.go
76 lines (68 loc) · 1.7 KB
/
general.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
package utils
import (
"crypto/sha256"
"fmt"
"hash/fnv"
"os"
"strings"
"time"
)
func isTesting() bool {
currentPath, err := os.Getwd()
if err != nil {
return false
}
return strings.HasSuffix(currentPath, "_test")
}
// Calculate the SHA256 hash of a string
func Hash256(s string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))
}
// Calculate the SHA256 hash of a byte sequence
func Hash256FromBytes(b []byte) string {
return fmt.Sprintf("%x", sha256.Sum256(b))
}
// Calculate a numeric hash of a string
func HashNumber(source string) string {
h := fnv.New32a()
h.Write([]byte(source))
return fmt.Sprintf("%d", h.Sum32()+1000)[0:4]
}
// Returns all numbers from a string and only the numbers
func GetOnlyNumbers(str string) string {
var numbers string
for _, char := range str {
if char >= '0' && char <= '9' {
numbers += string(char)
}
}
return numbers
}
// Complete a string with left zeros until it reaches the desired length
func CompleteWithZeros(str string, length int) string {
for len(str) < length {
str = "0" + str
}
return str
}
// Returns a string list with the dates between the start and end dates, inclusive of both
// The dates must be in the format "2006-01-02"
func DatesInInterval(start string, end string) []string {
startDate, err := time.Parse("2006-01-02 15:04:05", start+" 00:00:00")
if err != nil {
return nil
}
endDate, err := time.Parse("2006-01-02 15:04:05", end+" 23:59:59")
if err != nil {
return nil
}
if startDate.After(endDate) {
return nil
}
var dates []string
for startDate.Before(endDate) || startDate.Equal(endDate) {
dates = append(dates, startDate.Format("2006-01-02"))
startDate = startDate.AddDate(0, 0, 1)
}
return dates
}