time.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. @Time : 2019-07-08 11:12
  3. @Author : zr
  4. */
  5. package utils
  6. import (
  7. "gframe/errors"
  8. "math"
  9. "math/rand"
  10. "time"
  11. )
  12. func TimeFormatter() string {
  13. return "2006-01-02 15:04:05"
  14. }
  15. func TimeFormatterDate() string {
  16. return "2006-01-02"
  17. }
  18. func TimeStdQueryBegin(stdTimeStr string) (newTime time.Time, err error) {
  19. if stdTimeStr == "" {
  20. return time.Parse(TimeFormatter(), "2000-01-01 00:00:00")
  21. }
  22. newTime, err = time.Parse(TimeFormatter(), stdTimeStr)
  23. if err != nil {
  24. return newTime, err
  25. }
  26. newTimeStr := newTime.Format("2006-01-02") + " 00:00:00"
  27. return time.Parse(TimeFormatter(), newTimeStr)
  28. }
  29. func TimeStdQueryEnd(stdTimeStr string) (newTime time.Time, err error) {
  30. if stdTimeStr == "" {
  31. newTime = time.Now()
  32. } else {
  33. newTime, err = time.Parse(TimeFormatter(), stdTimeStr)
  34. if err != nil {
  35. return newTime, err
  36. }
  37. }
  38. newTimeStr := newTime.Format("2006-01-02") + " 23:59:59"
  39. return time.Parse(TimeFormatter(), newTimeStr)
  40. }
  41. func TimeDayBegin(day time.Time) time.Time {
  42. dayBeginStr := day.Format("2006-01-02") + " 00:00:00"
  43. dayBegin, _ := time.Parse(TimeFormatter(), dayBeginStr)
  44. return dayBegin
  45. }
  46. func TimeTodayBegin() time.Time {
  47. timeNow := time.Now()
  48. dayBeginStr := timeNow.Format("2006-01-02") + " 00:00:00"
  49. dayBegin, _ := time.Parse(TimeFormatter(), dayBeginStr)
  50. return dayBegin
  51. }
  52. func TimeDayEnd(day time.Time) time.Time {
  53. dayStr := day.Format("2006-01-02") + " 23:59:59"
  54. day, _ = time.Parse(TimeFormatter(), dayStr)
  55. return day
  56. }
  57. func RandomInt(n int) int {
  58. min := int(math.Pow(10, float64(n-1)))
  59. max := min*9 - 1
  60. rand1 := rand.New(rand.NewSource(time.Now().UnixNano()))
  61. code := rand1.Intn(max) + min
  62. return code
  63. }
  64. func GetAgeByBirthday(b time.Time) (age int) {
  65. //b, err := time.Parse(TimeFormatter(), birthday)
  66. //if err != nil {
  67. // return 0, err
  68. //}
  69. year := b.Year()
  70. month := int(b.Month())
  71. day := b.Day()
  72. nowYear := time.Now().Year()
  73. nowMonth := int(time.Now().Month())
  74. nowDay := time.Now().Day()
  75. if nowYear < year {
  76. panic(errors.ErrBirthday)
  77. }
  78. age = nowYear - year
  79. if month > nowMonth { // 当生日月份>当前月份的时候,年龄-1
  80. age = age - 1
  81. }
  82. if month == nowMonth && day > nowDay {
  83. age = age - 1 // 当生日月份与当前月份相等的时候,如果生日日期大于当天日期,则生日未到,年龄-1
  84. }
  85. return
  86. }
  87. func GetStaticHrByAge(age int) (staticHr int) {
  88. switch age {
  89. case 2:
  90. staticHr = 80
  91. case 3:
  92. staticHr = 80
  93. case 4:
  94. staticHr = 80
  95. case 5:
  96. staticHr = 75
  97. case 6:
  98. staticHr = 75
  99. case 7:
  100. staticHr = 70
  101. case 8:
  102. staticHr = 70
  103. case 9:
  104. staticHr = 70
  105. default:
  106. if age >= 10 {
  107. staticHr = 60
  108. } else {
  109. panic(errors.ErrStaticHr)
  110. }
  111. }
  112. return
  113. }