main.go 734 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. s := "2023-12-30"
  8. //4BC6 [4,11,12,6]
  9. t, err := time.Parse("2006-01-02", s)
  10. if err != nil {
  11. fmt.Println(err)
  12. return
  13. }
  14. y := Uint2BCD(uint64(t.Year()), true)
  15. fmt.Println("year:", y)
  16. m := Uint2BCD(uint64(t.Month()), true)
  17. fmt.Println("mouth:", m)
  18. d := Uint2BCD(uint64(t.Day()), true)
  19. fmt.Println("day:", d)
  20. }
  21. func Uint2BCD(n uint64, isBigEndian bool) []byte {
  22. var b []byte
  23. //if n < 256 {
  24. // b = []byte{0}
  25. //}
  26. for i := 0; ; i++ {
  27. h := (n / 10) % 10
  28. l := n % 10
  29. b = append(b, byte(h<<4|l))
  30. n = n / 100
  31. if n == 0 {
  32. break
  33. }
  34. }
  35. if !isBigEndian {
  36. return b
  37. }
  38. l := len(b)
  39. var r = make([]byte, l)
  40. for i, v := range b {
  41. r[l-1-i] = v
  42. }
  43. return r
  44. }