| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package utils
- import (
- "MeterService/core/constant"
- "time"
- )
- // IsBigEnd 判断是否大端
- func IsBigEnd() bool {
- var (
- v3 uint32
- b3 [4]byte
- )
- v3 = 257
- b3[0] = uint8(v3)
- b3[1] = uint8(v3 >> 8)
- b3[2] = uint8(v3 >> 16)
- b3[3] = uint8(v3 >> 24)
- if b3[0] == 1 {
- return false
- }
- return true
- }
- func IntoByte(c int) byte {
- var (
- b3 [4]byte
- )
- b3[0] = uint8(c)
- b3[1] = uint8(c >> 8)
- b3[2] = uint8(c >> 16)
- b3[3] = uint8(c >> 24)
- if IsBigEnd() {
- return b3[3]
- }
- return b3[0]
- }
- // NowDataToTimeStamp 获取当前时间戳
- func NowDataToTimeStamp() int64 {
- v := time.Now()
- return time.Date(v.Year(), v.Month(), v.Day(), v.Hour(), v.Minute(), 0, 0, time.Local).Unix()
- }
- func ConnTimeoutJudge(tm int64) bool {
- return TimeDiff(tm, int64(constant.TCPTimeoutTime))
- }
- // TimeDiff 时间差计算,true表示超过时间
- func TimeDiff(tm, mat int64) bool {
- if time.Now().Unix()-tm > mat {
- return true
- } else {
- return false
- }
- }
- // HfWord2byte 将一个int16转换为两个byte
- func HfWord2byte(obj int) (slice []byte) {
- slice = append(slice, IntoByte(obj>>8))
- slice = append(slice, IntoByte(obj&0xFF))
- return
- }
- // Word2byte 将一个int转换为四个byte
- func Word2byte(obj int) (slice []byte) {
- slice = append(slice, IntoByte(obj>>24))
- slice = append(slice, IntoByte(obj>>16))
- slice = append(slice, IntoByte(obj>>8))
- slice = append(slice, IntoByte(obj&0xFF))
- return
- }
|