-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
46 lines (38 loc) · 897 Bytes
/
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
package qless
import (
"crypto/rand"
"encoding/hex"
"errors"
"io"
"time"
"unicode"
"unicode/utf8"
)
var uuidRand = rand.Reader
func generateUUID() (string, error) {
var uuid [16]byte
if n, err := io.ReadFull(uuidRand, uuid[:]); err != nil {
return "", errors.New("read failed: " + err.Error())
} else if n != len(uuid) {
return "", errors.New("read failed")
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
var dst [32]byte
hex.Encode(dst[:], uuid[:])
return string(dst[:]), nil
}
// returns a timestamp used in LUA calls
func timestamp() int64 {
return time.Now().Unix()
}
// makes the first character of a string upper case
func ucfirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[n:]
}