-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.go
119 lines (101 loc) · 2.35 KB
/
number.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
package utils
import (
"math/rand"
"time"
)
// 获取本地时间
func CovertTime(timestamp int64) time.Time {
return time.Unix(timestamp, 0)
}
func String2Time(s string) time.Time {
loc, _ := time.LoadLocation("Local")
t, _ := time.ParseInLocation("2006-01-02 15:04:05", s, loc)
return t
}
func String2TimeD(s string) time.Time {
loc, _ := time.LoadLocation("Local")
t, _ := time.ParseInLocation("2006-01-02", s, loc)
return t
}
// 时间转换 -> 天
func Time2StringD(t time.Time) string {
return t.Format("2006-01-02")
}
// 获取当前的时间
func Time2StringByNow() string {
t := GetNowDayFinishTime()
return t.Format("20060102")
}
// 时间转换 -> 小时
func Time2StringH(t time.Time) string {
return t.Format("2006-01-02 15")
}
// 时间转换 -> 分钟
func Time2StringM(t time.Time) string {
return t.Format("2006-01-02 15:04")
}
// 时间转换 -> 秒
func Time2StringS(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
// 获取秒数据
func GetSecond() int64 {
return time.Now().Unix()
}
// 获取毫秒数据
func GetNSecond() int64 {
return time.Now().UnixNano() / 1e6
}
// 获取纳秒数据
func GetNNSecond() int64 {
return time.Now().UnixNano()
}
// 获取具体时间String
func GetTimeString() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// 获取当天的起始时间
func GetNowDayStartTime() time.Time {
t := GetSecond() - 24*3600
return CovertTime(t)
}
// 获取当天的结束时间
func GetNowDayFinishTime() time.Time {
t := GetSecond()
return CovertTime(t)
}
func Abs(n int64) int64 {
if n < 0 {
return (-1) * n
}
return n
}
func AbsFloat(n float64) float64 {
if n < 0 {
return (-1) * n
}
return n
}
func rapidRandom() string {
// 生成的UUID, 参考java的UUID实现
str := "0123456789abcdefghijklmnopqrstuvwxyz"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 16; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
result[6] &= 0x0f /* clear version */
result[6] |= 0x40 /* set to version 4 */
result[8] &= 0x3f /* clear variant */
result[8] |= 0x80 /* set to IETF variant */
var msb, lsb int64
for i := 0; i < 8; i++ {
msb = (msb << 8) | int64(result[i]&0xff)
}
for i := 8; i < 16; i++ {
lsb = (lsb << 8) | int64(result[i]&0xff)
}
v := Abs(msb ^ lsb)
return Int642String(v)
}