工具包
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package zhios_tool_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 TimeToStr(unixSecTime interface{}, layout ...string) string {
  37. i := AnyToInt64(unixSecTime)
  38. if i == 0 {
  39. return ""
  40. }
  41. f := "2006-01-02 15:04:05"
  42. if len(layout) > 0 {
  43. f = layout[0]
  44. }
  45. return time.Unix(i, 0).Format(f)
  46. }
  47. func FormatNanoUnix() string {
  48. return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1)
  49. }
  50. func TimeParse(format, src string) (time.Time, error) {
  51. return time.ParseInLocation(format, src, time.Local)
  52. }
  53. func TimeParseStd(src string) time.Time {
  54. t, _ := TimeParse("2006-01-02 15:04:05", src)
  55. return t
  56. }
  57. func TimeStdParseUnix(src string) int64 {
  58. t, err := TimeParse("2006-01-02 15:04:05", src)
  59. if err != nil {
  60. return 0
  61. }
  62. return t.Unix()
  63. }
  64. // 获取一个当前时间 时间间隔 时间戳
  65. func GetTimeInterval(unit string, amount int) (startTime, endTime int64) {
  66. t := time.Now()
  67. nowTime := t.Unix()
  68. tmpTime := int64(0)
  69. switch unit {
  70. case "years":
  71. tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  72. case "months":
  73. tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  74. case "days":
  75. tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix()
  76. case "hours":
  77. tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix()
  78. }
  79. if amount > 0 {
  80. startTime = nowTime
  81. endTime = tmpTime
  82. } else {
  83. startTime = tmpTime
  84. endTime = nowTime
  85. }
  86. return
  87. }
  88. // 几天前
  89. func TimeInterval(newTime int) string {
  90. now := time.Now().Unix()
  91. newTime64 := AnyToInt64(newTime)
  92. if newTime64 >= now {
  93. return "刚刚"
  94. }
  95. interval := now - newTime64
  96. switch {
  97. case interval < 60:
  98. return AnyToString(interval) + "秒前"
  99. case interval < 60*60:
  100. return AnyToString(interval/60) + "分前"
  101. case interval < 60*60*24:
  102. return AnyToString(interval/60/60) + "小时前"
  103. case interval < 60*60*24*30:
  104. return AnyToString(interval/60/60/24) + "天前"
  105. case interval < 60*60*24*30*12:
  106. return AnyToString(interval/60/60/24/30) + "月前"
  107. default:
  108. return AnyToString(interval/60/60/24/30/12) + "年前"
  109. }
  110. }
  111. // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10
  112. func HmsToUnix(str string) (int64, error) {
  113. t := time.Now()
  114. arr := strings.Split(str, ":")
  115. if len(arr) < 2 {
  116. return 0, errors.New("Time format error")
  117. }
  118. h, _ := strconv.Atoi(arr[0])
  119. m, _ := strconv.Atoi(arr[1])
  120. s := 0
  121. if len(arr) == 3 {
  122. s, _ = strconv.Atoi(arr[3])
  123. }
  124. formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s)
  125. res, err := time.ParseInLocation("20060102150405", formatted1, time.Local)
  126. if err != nil {
  127. return 0, err
  128. } else {
  129. return res.Unix(), nil
  130. }
  131. }
  132. // 获取特定时间范围
  133. func GetTimeRange(s string) map[string]int64 {
  134. t := time.Now()
  135. var stime, etime time.Time
  136. switch s {
  137. case "today":
  138. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  139. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  140. case "yesterday":
  141. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  142. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  143. case "within_seven_days":
  144. // 前6天0点
  145. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  146. // 明天 0点
  147. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  148. case "current_month":
  149. stime = GetFirstDateOfMonth(t)
  150. etime = time.Now()
  151. case "last_month":
  152. etime = GetFirstDateOfMonth(t)
  153. monthTimes := TimeStdParseUnix(etime.Format("2006-01-02 15:04:05")) - 86400
  154. times, _ := UnixToTime(Int64ToStr(monthTimes))
  155. stime = GetFirstDateOfMonth(times)
  156. }
  157. return map[string]int64{
  158. "start": stime.Unix(),
  159. "end": etime.Unix(),
  160. }
  161. }
  162. //时间戳转时间格式
  163. func UnixToTime(e string) (datatime time.Time, err error) {
  164. data, err := strconv.ParseInt(e, 10, 64)
  165. datatime = time.Unix(data, 0)
  166. return
  167. }
  168. //获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  169. func GetFirstDateOfMonth(d time.Time) time.Time {
  170. d = d.AddDate(0, 0, -d.Day()+1)
  171. return GetZeroTime(d)
  172. }
  173. //获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  174. func GetLastDateOfMonth(d time.Time) time.Time {
  175. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  176. }
  177. //获取某一天的0点时间
  178. func GetZeroTime(d time.Time) time.Time {
  179. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  180. }
  181. //获取当月某天的某个时间的时间
  182. func GetDayToTime(day, timeStr string) string {
  183. if timeStr == "" {
  184. timeStr = "00:00:00"
  185. }
  186. year := time.Now().Year()
  187. month := time.Now().Format("01")
  188. times := fmt.Sprintf("%s-%s-%s %s", IntToStr(year), month, day, timeStr)
  189. return times
  190. }