utils.go 681 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utils
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "unsafe"
  7. "encoding/json"
  8. )
  9. // StrToBytes 快读的string转byte
  10. func StrToBytes(s string) []byte {
  11. x := (*[2]uintptr)(unsafe.Pointer(&s))
  12. h := [3]uintptr{x[0], x[1], x[1]}
  13. return *(*[]byte)(unsafe.Pointer(&h))
  14. }
  15. func PrintStruct(prefix string, data interface{}) {
  16. bytes, _ := json.Marshal(data)
  17. fmt.Printf("%s, %s\n", prefix, bytes)
  18. }
  19. func GetMonthFromDate(date string) (int) {
  20. if date == "" {
  21. return -1
  22. }
  23. array := strings.Split(date, " ")
  24. if len(array) == 0 {
  25. return -1
  26. }
  27. date = array[0]
  28. t, err := time.ParseInLocation("2006-01-02", date, time.Local)
  29. if err != nil {
  30. return -1
  31. }
  32. return int(t.Month())
  33. }