-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelper.go
45 lines (41 loc) · 876 Bytes
/
helper.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
package main
import (
"archive/zip"
"bytes"
"fmt"
"strconv"
"time"
)
func randomString(strlen int) string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := range result {
result[i] = chars[r.Intn(len(chars))]
}
return string(result)
}
func createZip(filename string, content string) ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
f, err := w.Create(filename)
if err != nil {
return nil, err
}
if _, err = f.Write([]byte(content)); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func dateToString(in string) string {
i, err := strconv.ParseInt(in, 10, 64)
if err != nil {
return fmt.Sprintf("could not parse date %q: %v", in, err)
}
if i == 0 {
return ""
}
return time.Unix(i, 0).Local().Format(time.ANSIC)
}