common.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "strconv"
  6. xtime "time"
  7. )
  8. // WaitTime 计算等待时长
  9. func WaitTime(ctime xtime.Time) string {
  10. wt := xtime.Since(ctime)
  11. h := int(wt.Hours())
  12. m := int(wt.Minutes()) % 60
  13. s := int(wt.Seconds()) % 60
  14. return fmt.Sprintf("%.2d:%.2d:%.2d", h, m, s)
  15. }
  16. //IntTime .
  17. type IntTime int64
  18. // Scan scan time.
  19. func (jt *IntTime) Scan(src interface{}) (err error) {
  20. switch sc := src.(type) {
  21. case xtime.Time:
  22. *jt = IntTime(sc.Unix())
  23. case string:
  24. var i int64
  25. i, err = strconv.ParseInt(sc, 10, 64)
  26. *jt = IntTime(i)
  27. }
  28. return
  29. }
  30. // Value get time value.
  31. func (jt IntTime) Value() (driver.Value, error) {
  32. return xtime.Unix(int64(jt), 0), nil
  33. }
  34. // Time get time.
  35. func (jt IntTime) Time() xtime.Time {
  36. return xtime.Unix(int64(jt), 0)
  37. }
  38. // UnmarshalJSON implement Unmarshaler
  39. func (jt *IntTime) UnmarshalJSON(data []byte) error {
  40. if data == nil || len(data) <= 1 {
  41. *jt = 0
  42. return nil
  43. }
  44. if data[0] != '"' {
  45. // 1.直接判断数字
  46. sti, err := strconv.Atoi(string(data))
  47. if err == nil {
  48. *jt = IntTime(sti)
  49. }
  50. return nil
  51. }
  52. str := string(data[1 : len(data)-1])
  53. // 2.标准格式判断
  54. st, err := xtime.ParseInLocation("2006-01-02 15:04:05", str, xtime.Local)
  55. if err == nil {
  56. *jt = IntTime(st.Unix())
  57. return nil
  58. }
  59. *jt = IntTime(0)
  60. return nil
  61. }
  62. //BaseResponse .
  63. type BaseResponse struct {
  64. Code int64 `json:"code"`
  65. Message string `json:"message"`
  66. }