util.go 920 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package util
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. const (
  7. LocationName = "Asia/Shanghai"
  8. LayoutTime = "2006-01-02 15:04:05"
  9. Layout = "2006-01-02"
  10. )
  11. // TimeParseLocalUnix 获取当天零点的时间戳
  12. // eg 2023-02-22 => 1676995200
  13. func TimeParseLocalUnix(DayTime string) int64 {
  14. value := DayTime
  15. if len(DayTime) <= 11 {
  16. value = fmt.Sprintf("%s 00:00:00", DayTime)
  17. }
  18. loc, _ := time.LoadLocation("Local")
  19. theTime, _ := time.ParseInLocation(LayoutTime, value, loc)
  20. return theTime.Unix()
  21. }
  22. // ConvertParseLocalUnix 字符串转换当天时间戳
  23. // eg 15:04:05 => 1676998245
  24. func ConvertParseLocalUnix(timeParse string) (int64, error) {
  25. loc, err := time.LoadLocation("Local")
  26. if err != nil {
  27. return 0, err
  28. }
  29. value := fmt.Sprintf("%s %s", time.Now().Format(Layout), timeParse)
  30. theTime, err := time.ParseInLocation(LayoutTime, value, loc)
  31. if err != nil {
  32. return 0, err
  33. }
  34. return theTime.Unix(), nil
  35. }