util.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }
  36. // GetMonthRemainDay 获取当前月还剩几天
  37. func GetMonthRemainDay() int {
  38. now := time.Now()
  39. lastDayOfMonth := time.Date(now.Year(), now.Month()+1, 0, 23, 59, 59, 999999999, now.Location())
  40. return int(lastDayOfMonth.Sub(now).Hours()/24) + 1
  41. }