-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_utils.go
159 lines (143 loc) · 2.53 KB
/
global_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"fmt"
"github.com/snabb/isoweek"
"os"
"time"
)
/* list equal check */
func isStringListEqual(x, y []string) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isIntListEqual(x, y []int) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isInt64ListEqual(x, y []int64) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isUintListEqual(x, y []uint) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isUint64ListEqual(x, y []uint64) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isFloat32ListEqual(x, y []float32) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isFloat64ListEqual(x, y []float64) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if x[index] != y[index] {
return false
}
}
return true
}
func isFloat64List2DEqual(x, y [][]float64) bool {
if len(x) != len(y) {
return false
}
for index := range x {
if !isFloat64ListEqual(x[index], y[index]) {
return false
}
}
return true
}
/* time operations */
func getCurrentTimeStampUnixMillis() uint64 {
return uint64(time.Now().UnixNano() / 1000000)
}
func getSecondsOfWeek(ts time.Time) uint64 {
year, week := ts.ISOWeek()
weekStart := isoweek.StartTime(year, week, time.Local)
weekDuration := ts.Sub(weekStart)
return uint64(weekDuration.Seconds())
}
/* string operations */
func cToGoString(c []byte) string {
n := bytes.IndexByte(c, 0)
if n < 0 {
n = len(c)
}
return string(c[:n])
}
/* file operations */
func saveFile(name string, data []byte) error {
if data == nil {
return fmt.Errorf("empty data given")
}
f, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
if _, err := f.Write(data); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}
func moveFile(source string, destination string) error {
err := os.Rename(source, destination)
return err
}
func getFileSize(file string) int64 {
fi, e := os.Stat(file)
if e != nil {
return 0
}
return fi.Size()
}