time.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package common
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // NowTime 时间格式化
  9. func NowTime() string {
  10. return time.Now().Format(`2006-01-02 15:04:05`)
  11. }
  12. // GetHexTimeStr 时间16进制字符串
  13. func GetHexTimeStr() string {
  14. nowtime := time.Now().Format(`2006-01-02 15:04:05`)
  15. nowtime = "2023-07-31 14:28:00"
  16. nowtime = nowtime[2:]
  17. split := strings.Split(nowtime, " ")
  18. yearArr := strings.Split(split[0], "-")
  19. timeArr := strings.Split(split[1], ":")
  20. var result strings.Builder
  21. for _, value := range yearArr {
  22. valueInt, err := strconv.ParseInt(value, 10, 64)
  23. if err != nil {
  24. fmt.Errorf("%s", "data format error")
  25. return ""
  26. }
  27. formatInt := strconv.FormatInt(valueInt, 16)
  28. if len(formatInt) == 1 {
  29. formatInt = fmt.Sprintf("%s%s", "0", formatInt)
  30. }
  31. result.WriteString(formatInt)
  32. }
  33. for _, value := range timeArr {
  34. valueInt, err := strconv.ParseInt(value, 10, 64)
  35. if err != nil {
  36. fmt.Errorf("%s", "data format error")
  37. return ""
  38. }
  39. formatInt := strconv.FormatInt(valueInt, 16)
  40. if len(formatInt) == 1 {
  41. formatInt = fmt.Sprintf("%s%s", "0", formatInt)
  42. }
  43. result.WriteString(formatInt)
  44. }
  45. //for _, value := range timeArr {
  46. // valueInt, err := strconv.ParseInt(value, 10, 64)
  47. // if err != nil {
  48. // fmt.Errorf("%s", "data format error")
  49. // return ""
  50. // }
  51. // if valueInt < 10 {
  52. // result.WriteString("0")
  53. // }
  54. // result.WriteString(strconv.FormatInt(valueInt, 16))
  55. //}
  56. return result.String()
  57. }