time.go 1.5 KB

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