附近小店
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.
 
 
 

240 Zeilen
6.1 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 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. // 明天 0点
  145. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  146. stime = time.Unix(etime.Unix()-7*86400, 0)
  147. case "within_fifteen_days":
  148. // 明天 0点
  149. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  150. // 前14天0点
  151. stime = time.Unix(etime.Unix()-15*86400, 0)
  152. case "current_month":
  153. stime = GetFirstDateOfMonth(t)
  154. etime = time.Now()
  155. case "last_month":
  156. etime = GetFirstDateOfMonth(t)
  157. monthTimes := TimeStdParseUnix(etime.Format("2006-01-02 15:04:05")) - 86400
  158. times, _ := UnixToTime(Int64ToStr(monthTimes))
  159. stime = GetFirstDateOfMonth(times)
  160. }
  161. return map[string]int64{
  162. "start": stime.Unix(),
  163. "end": etime.Unix(),
  164. }
  165. }
  166. /**
  167. 获取本周周一的日期
  168. */
  169. func GetFirstDateOfWeek() (weekMonday string) {
  170. now := time.Now()
  171. offset := int(time.Monday - now.Weekday())
  172. if offset > 0 {
  173. offset = -6
  174. }
  175. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  176. weekMonday = weekStartDate.Format("2006-01-02")
  177. return
  178. }
  179. /**
  180. 获取上周的周一日期
  181. */
  182. func GetLastWeekFirstDate() (weekMonday string) {
  183. thisWeekMonday := GetFirstDateOfWeek()
  184. TimeMonday, _ := time.Parse("2006-01-02", thisWeekMonday)
  185. lastWeekMonday := TimeMonday.AddDate(0, 0, -7)
  186. weekMonday = lastWeekMonday.Format("2006-01-02")
  187. return
  188. }
  189. //时间戳转时间格式
  190. func UnixToTime(e string) (datatime time.Time, err error) {
  191. data, err := strconv.ParseInt(e, 10, 64)
  192. datatime = time.Unix(data, 0)
  193. return
  194. }
  195. //获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  196. func GetFirstDateOfMonth(d time.Time) time.Time {
  197. d = d.AddDate(0, 0, -d.Day()+1)
  198. return GetZeroTime(d)
  199. }
  200. //获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  201. func GetLastDateOfMonth(d time.Time) time.Time {
  202. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  203. }
  204. //获取某一天的0点时间
  205. func GetZeroTime(d time.Time) time.Time {
  206. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  207. }
  208. //获取当月某天的某个时间的时间
  209. func GetDayToTime(day, timeStr string) string {
  210. if timeStr == "" {
  211. timeStr = "00:00:00"
  212. }
  213. year := time.Now().Year()
  214. month := time.Now().Format("01")
  215. times := fmt.Sprintf("%s-%s-%s %s", IntToStr(year), month, day, timeStr)
  216. return times
  217. }