Skip to content

Commit 277d4f7

Browse files
committed
在convert中提供方便一次性格式化的函数
1 parent 66db333 commit 277d4f7

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

convert.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package bilog
22

3-
import "strconv"
3+
import (
4+
"reflect"
5+
"strconv"
6+
"unsafe"
7+
)
48

59
// 快速格式化年月日
610
// year之外的数字如果小于10则会使用零填充prefix
@@ -96,3 +100,31 @@ func fastConvertMinute(i int) string {
96100
func fastConvertSecond(i int) string {
97101
return secondBuf[i]
98102
}
103+
104+
// 返回存储时间的字节数组和写入数据的有效数量
105+
func fastConvertAllToArray(year, month, day, hour, minute, second int) (tmp [32]byte, eff int) {
106+
data := (uintptr)(unsafe.Pointer(&tmp))
107+
slice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
108+
Data: data,
109+
Len: 0,
110+
Cap: len(tmp),
111+
}))
112+
slice = append(slice, fastConvertYear(year)...)
113+
slice = append(slice, fastConvertMonth(month)...)
114+
slice = append(slice, fastConvertDay(day)...)
115+
slice = append(slice, fastConvertHour(hour)...)
116+
slice = append(slice, fastConvertMinute(minute)...)
117+
slice = append(slice, fastConvertSecond(second)...)
118+
return tmp, len(slice)
119+
}
120+
121+
func fastConvertAllToSlice(year, month, day, hour, minute, second int) []byte {
122+
tmp := make([]byte, 0, 32)
123+
tmp = append(tmp, fastConvertYear(year)...)
124+
tmp = append(tmp, fastConvertMonth(month)...)
125+
tmp = append(tmp, fastConvertDay(day)...)
126+
tmp = append(tmp, fastConvertHour(hour)...)
127+
tmp = append(tmp, fastConvertMinute(minute)...)
128+
tmp = append(tmp, fastConvertSecond(second)...)
129+
return tmp
130+
}

0 commit comments

Comments
 (0)