Files
injoyai-tdx/protocol/unit.go

118 lines
2.5 KiB
Go

package protocol
import (
"bytes"
"fmt"
bytes2 "github.com/injoyai/base/bytes"
"github.com/injoyai/conv"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"time"
)
// String 字节先转小端,再转字符
func String(bs []byte) string {
return string(bytes2.Reverse(bs))
}
// Bytes 任意类型转小端字节
func Bytes(n any) []byte {
return bytes2.Reverse(conv.Bytes(n))
}
// Reverse 字节倒序
func Reverse(bs []byte) []byte {
x := make([]byte, len(bs))
for i, v := range bs {
x[len(bs)-i-1] = v
}
return x
}
// Uint32 字节通过小端方式转为uint32
func Uint32(bs []byte) uint32 {
return conv.Uint32(bytes2.Reverse(bs))
}
// Uint16 字节通过小端方式转为uint16
func Uint16(bs []byte) uint16 {
return conv.Uint16(bytes2.Reverse(bs))
}
func UTF8ToGBK(text []byte) []byte {
r := bytes.NewReader(text)
decoder := transform.NewReader(r, simplifiedchinese.GBK.NewDecoder()) //GB18030
content, _ := io.ReadAll(decoder)
return bytes.ReplaceAll(content, []byte{0x00}, []byte{})
}
func FloatUnit(f float64) (float64, string) {
m := []string{"万", "亿"}
unit := ""
for i := 0; f > 1e4 && i < len(m); f /= 1e4 {
unit = m[i]
}
return f, unit
}
func FloatUnitString(f float64) string {
m := []string{"万", "亿", "万亿", "亿亿", "万亿亿", "亿亿亿"}
unit := ""
for i := 0; f > 1e4 && i < len(m); i++ {
unit = m[i]
f /= 1e4
}
if unit == "" {
return conv.String(f)
}
return fmt.Sprintf("%0.2f%s", f, unit)
}
func IntUnitString(n int) string {
return FloatUnitString(float64(n))
}
func GetDate(bs [2]byte) string {
n := Uint16(bs[:])
h := n / 60
m := n % 60
return fmt.Sprintf("%02d:%02d", h, m)
}
func GetTime(bs [4]byte, Type TypeKline) time.Time {
switch Type {
case TypeKlineDay, TypeKlineMinute, TypeKlineMinute2:
yearMonthDay := Uint16(bs[:2])
hourMinute := Uint16(bs[:2])
year := int(yearMonthDay>>11 + 2004)
month := yearMonthDay % 2048 / 100
day := int((yearMonthDay % 2048) % 100)
hour := int(hourMinute / 60)
minute := int(hourMinute % 60)
return time.Date(year, time.Month(month), day, hour, minute, 0, 0, time.Local)
default:
yearMonthDay := Uint32(bs[:4])
year := int(yearMonthDay / 10000)
month := int((yearMonthDay % 10000) / 100)
day := int(yearMonthDay % 100)
return time.Date(year, time.Month(month), day, 15, 0, 0, 0, time.Local)
}
}
func basePrice(code string) Price {
if len(code) == 0 {
return 1
}
switch code[:2] {
case "60", "30", "68", "00":
return 1
default:
return 10
}
}