golang 的 rabbitmq 消费项目
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

182 рядки
4.6 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. func StrToTime(s string) (int64, error) {
  10. // delete all not int characters
  11. if s == "" {
  12. return time.Now().Unix(), nil
  13. }
  14. r := make([]rune, 14)
  15. l := 0
  16. // 过滤除数字以外的字符
  17. for _, v := range s {
  18. if '0' <= v && v <= '9' {
  19. r[l] = v
  20. l++
  21. if l == 14 {
  22. break
  23. }
  24. }
  25. }
  26. for l < 14 {
  27. r[l] = '0' // 补0
  28. l++
  29. }
  30. t, err := time.Parse("20060102150405", string(r))
  31. if err != nil {
  32. return 0, err
  33. }
  34. return t.Unix(), nil
  35. }
  36. func Time2String(date time.Time, format string) string {
  37. if format == "" {
  38. format = "2006-01-02 15:04:05"
  39. }
  40. timeS := date.Format(format)
  41. if timeS == "0001-01-01 00:00:00" || strings.Contains(timeS, "0001-01-01") {
  42. return ""
  43. }
  44. return timeS
  45. }
  46. func String2Time(timeStr string) time.Time {
  47. toTime, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
  48. if err != nil {
  49. return time.Now()
  50. }
  51. return toTime
  52. }
  53. func TimeToStr(unixSecTime interface{}, layout ...string) string {
  54. i := AnyToInt64(unixSecTime)
  55. if i == 0 {
  56. return ""
  57. }
  58. f := "2006-01-02 15:04:05"
  59. if len(layout) > 0 {
  60. f = layout[0]
  61. }
  62. return time.Unix(i, 0).Format(f)
  63. }
  64. func FormatNanoUnix() string {
  65. return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1)
  66. }
  67. func TimeParse(format, src string) (time.Time, error) {
  68. return time.ParseInLocation(format, src, time.Local)
  69. }
  70. func TimeParseStd(src string) time.Time {
  71. t, _ := TimeParse("2006-01-02 15:04:05", src)
  72. return t
  73. }
  74. func TimeStdParseUnix(src string) int64 {
  75. t, err := TimeParse("2006-01-02 15:04:05", src)
  76. if err != nil {
  77. return 0
  78. }
  79. return t.Unix()
  80. }
  81. // 获取一个当前时间 时间间隔 时间戳
  82. func GetTimeInterval(unit string, amount int) (startTime, endTime int64) {
  83. t := time.Now()
  84. nowTime := t.Unix()
  85. tmpTime := int64(0)
  86. switch unit {
  87. case "years":
  88. tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  89. case "months":
  90. tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  91. case "days":
  92. tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix()
  93. case "hours":
  94. tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix()
  95. }
  96. if amount > 0 {
  97. startTime = nowTime
  98. endTime = tmpTime
  99. } else {
  100. startTime = tmpTime
  101. endTime = nowTime
  102. }
  103. return
  104. }
  105. // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10
  106. func HmsToUnix(str string) (int64, error) {
  107. t := time.Now()
  108. arr := strings.Split(str, ":")
  109. if len(arr) < 2 {
  110. return 0, errors.New("Time format error")
  111. }
  112. h, _ := strconv.Atoi(arr[0])
  113. m, _ := strconv.Atoi(arr[1])
  114. s := 0
  115. if len(arr) == 3 {
  116. s, _ = strconv.Atoi(arr[3])
  117. }
  118. formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s)
  119. res, err := time.ParseInLocation("20060102150405", formatted1, time.Local)
  120. if err != nil {
  121. return 0, err
  122. } else {
  123. return res.Unix(), nil
  124. }
  125. }
  126. // 获取特定时间范围
  127. func GetTimeRange(s string) map[string]int64 {
  128. t := time.Now()
  129. var stime, etime time.Time
  130. switch s {
  131. case "today":
  132. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  133. // 明天 0点
  134. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  135. case "this_month":
  136. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  137. // 这个月的最后一天
  138. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  139. case "last_month":
  140. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  141. // 上月的最后一天
  142. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  143. }
  144. return map[string]int64{
  145. "start": stime.Unix(),
  146. "end": etime.Unix(),
  147. }
  148. }
  149. // GetFirstDateOfMonth 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  150. func GetFirstDateOfMonth(d time.Time) time.Time {
  151. d = d.AddDate(0, 0, -d.Day()+1)
  152. return GetZeroTime(d)
  153. }
  154. // GetLastDateOfMonth 获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  155. func GetLastDateOfMonth(d time.Time) time.Time {
  156. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  157. }
  158. // GetZeroTime 获取某一天的0点时间
  159. func GetZeroTime(d time.Time) time.Time {
  160. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  161. }
  162. // GetZeroTime 获取某一天的0点时间的时间戳
  163. func GetZeroTimeUnix(d time.Time) int64 {
  164. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()).Unix()
  165. }