util.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package utils
  2. import (
  3. "MeterService/core/constant"
  4. "time"
  5. )
  6. // IsBigEnd 判断是否大端
  7. func IsBigEnd() bool {
  8. var (
  9. v3 uint32
  10. b3 [4]byte
  11. )
  12. v3 = 257
  13. b3[0] = uint8(v3)
  14. b3[1] = uint8(v3 >> 8)
  15. b3[2] = uint8(v3 >> 16)
  16. b3[3] = uint8(v3 >> 24)
  17. if b3[0] == 1 {
  18. return false
  19. }
  20. return true
  21. }
  22. func IntoByte(c int) byte {
  23. var (
  24. b3 [4]byte
  25. )
  26. b3[0] = uint8(c)
  27. b3[1] = uint8(c >> 8)
  28. b3[2] = uint8(c >> 16)
  29. b3[3] = uint8(c >> 24)
  30. if IsBigEnd() {
  31. return b3[3]
  32. }
  33. return b3[0]
  34. }
  35. // NowDataToTimeStamp 获取当前时间戳
  36. func NowDataToTimeStamp() int64 {
  37. v := time.Now()
  38. return time.Date(v.Year(), v.Month(), v.Day(), v.Hour(), v.Minute(), 0, 0, time.Local).Unix()
  39. }
  40. func ConnTimeoutJudge(tm int64) bool {
  41. return TimeDiff(tm, int64(constant.TCPTimeoutTime))
  42. }
  43. // TimeDiff 时间差计算,true表示超过时间
  44. func TimeDiff(tm, mat int64) bool {
  45. if time.Now().Unix()-tm > mat {
  46. return true
  47. } else {
  48. return false
  49. }
  50. }
  51. // HfWord2byte 将一个int16转换为两个byte
  52. func HfWord2byte(obj int) (slice []byte) {
  53. slice = append(slice, IntoByte(obj>>8))
  54. slice = append(slice, IntoByte(obj&0xFF))
  55. return
  56. }
  57. // Word2byte 将一个int转换为四个byte
  58. func Word2byte(obj int) (slice []byte) {
  59. slice = append(slice, IntoByte(obj>>24))
  60. slice = append(slice, IntoByte(obj>>16))
  61. slice = append(slice, IntoByte(obj>>8))
  62. slice = append(slice, IntoByte(obj&0xFF))
  63. return
  64. }