优惠券额度包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 regels
7.9 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" {
  42. return ""
  43. }
  44. return timeS
  45. }
  46. func TimeToStr(unixSecTime interface{}, layout ...string) string {
  47. i := AnyToInt64(unixSecTime)
  48. if i == 0 {
  49. return ""
  50. }
  51. f := "2006-01-02 15:04:05"
  52. if len(layout) > 0 {
  53. f = layout[0]
  54. }
  55. return time.Unix(i, 0).Format(f)
  56. }
  57. func FormatNanoUnix() string {
  58. return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1)
  59. }
  60. func TimeParse(format, src string) (time.Time, error) {
  61. return time.ParseInLocation(format, src, time.Local)
  62. }
  63. func TimeParseStd(src string) time.Time {
  64. t, _ := TimeParse("2006-01-02 15:04:05", src)
  65. return t
  66. }
  67. func TimeStdParseUnix(src string) int64 {
  68. t, err := TimeParse("2006-01-02 15:04:05", src)
  69. if err != nil {
  70. return 0
  71. }
  72. return t.Unix()
  73. }
  74. // 获取一个当前时间 时间间隔 时间戳
  75. func GetTimeInterval(unit string, amount int) (startTime, endTime int64) {
  76. t := time.Now()
  77. nowTime := t.Unix()
  78. tmpTime := int64(0)
  79. switch unit {
  80. case "years":
  81. tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  82. case "months":
  83. tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  84. case "days":
  85. tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix()
  86. case "hours":
  87. tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix()
  88. }
  89. if amount > 0 {
  90. startTime = nowTime
  91. endTime = tmpTime
  92. } else {
  93. startTime = tmpTime
  94. endTime = nowTime
  95. }
  96. return
  97. }
  98. // 几天前
  99. func TimeInterval(newTime int) string {
  100. now := time.Now().Unix()
  101. newTime64 := AnyToInt64(newTime)
  102. if newTime64 >= now {
  103. return "刚刚"
  104. }
  105. interval := now - newTime64
  106. switch {
  107. case interval < 60:
  108. return AnyToString(interval) + "秒前"
  109. case interval < 60*60:
  110. return AnyToString(interval/60) + "分前"
  111. case interval < 60*60*24:
  112. return AnyToString(interval/60/60) + "小时前"
  113. case interval < 60*60*24*30:
  114. return AnyToString(interval/60/60/24) + "天前"
  115. case interval < 60*60*24*30*12:
  116. return AnyToString(interval/60/60/24/30) + "月前"
  117. default:
  118. return AnyToString(interval/60/60/24/30/12) + "年前"
  119. }
  120. }
  121. // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10
  122. func HmsToUnix(str string) (int64, error) {
  123. t := time.Now()
  124. arr := strings.Split(str, ":")
  125. if len(arr) < 3 {
  126. return 0, errors.New("Time format error")
  127. }
  128. h, _ := strconv.Atoi(arr[0])
  129. m, _ := strconv.Atoi(arr[1])
  130. s := 0
  131. if len(arr) == 3 {
  132. s, _ = strconv.Atoi(arr[2])
  133. }
  134. formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s)
  135. res, err := time.ParseInLocation("20060102150405", formatted1, time.Local)
  136. if err != nil {
  137. return 0, err
  138. } else {
  139. return res.Unix(), nil
  140. }
  141. }
  142. // 获取特定时间范围
  143. func GetTimeRange(s string) map[string]int64 {
  144. t := time.Now()
  145. var stime, etime time.Time
  146. switch s {
  147. case "today":
  148. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  149. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  150. case "yesterday":
  151. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  152. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  153. case "within_seven_days":
  154. // 前6天0点
  155. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  156. // 明天 0点
  157. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  158. case "current_month":
  159. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  160. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  161. case "last_month":
  162. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  163. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  164. }
  165. return map[string]int64{
  166. "start": stime.Unix(),
  167. "end": etime.Unix(),
  168. }
  169. }
  170. // 获取特定时间范围
  171. func GetTimes(s string) map[string]string {
  172. t := time.Now()
  173. var stime, etime time.Time
  174. switch s {
  175. case "today":
  176. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  177. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  178. case "yesterday":
  179. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  180. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  181. case "within_seven_days":
  182. // 前6天0点
  183. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  184. // 明天 0点
  185. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  186. case "current_month":
  187. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  188. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  189. case "last_month":
  190. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  191. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  192. }
  193. return map[string]string{
  194. "start": stime.Format("2006-01-02 15:04:05"),
  195. "end": etime.Format("2006-01-02 15:04:05"),
  196. }
  197. }
  198. //获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  199. func GetFirstDateOfMonth(d time.Time) time.Time {
  200. d = d.AddDate(0, 0, -d.Day()+1)
  201. return GetZeroTime(d)
  202. }
  203. //获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  204. func GetLastDateOfMonth(d time.Time) time.Time {
  205. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  206. }
  207. func GetAnyFirstDateOfMonth(d time.Time, monthDiff int) time.Time {
  208. year, month, _ := d.Date()
  209. thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  210. monthOneDay := thisMonth.AddDate(0, monthDiff, 0)
  211. return monthOneDay
  212. }
  213. //当天时间戳
  214. func GetDateTime(date string) (int64, int64) {
  215. //获取当前时区
  216. loc, _ := time.LoadLocation("Local")
  217. //日期当天0点时间戳(拼接字符串)
  218. startDate := date + "_00:00:00"
  219. startTime, _ := time.ParseInLocation("2006-01-02_15:04:05", startDate, loc)
  220. //日期当天23时59分时间戳
  221. endDate := date + "_23:59:59"
  222. end, _ := time.ParseInLocation("2006-01-02_15:04:05", endDate, loc)
  223. //返回当天0点和23点59分的时间戳
  224. return startTime.Unix(), end.Unix()
  225. }
  226. //获取某一天的0点时间
  227. func GetZeroTime(d time.Time) time.Time {
  228. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  229. }
  230. // getYearMonthToDay 查询指定年份指定月份有多少天
  231. // @params year int 指定年份
  232. // @params month int 指定月份
  233. func GetYearMonthToDay(year int, month int) int {
  234. // 有31天的月份
  235. day31 := map[int]bool{
  236. 1: true,
  237. 3: true,
  238. 5: true,
  239. 7: true,
  240. 8: true,
  241. 10: true,
  242. 12: true,
  243. }
  244. if day31[month] == true {
  245. return 31
  246. }
  247. // 有30天的月份
  248. day30 := map[int]bool{
  249. 4: true,
  250. 6: true,
  251. 9: true,
  252. 11: true,
  253. }
  254. if day30[month] == true {
  255. return 30
  256. }
  257. // 计算是平年还是闰年
  258. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  259. // 得出2月的天数
  260. return 29
  261. }
  262. // 得出2月的天数
  263. return 28
  264. }
  265. // 获取两个时间相差的天数,0表同一天,正数表t1>t2,负数表t1<t2
  266. func GetDiffDays(t1, t2 time.Time) int {
  267. t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
  268. t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
  269. return int(t1.Sub(t2).Hours() / 24)
  270. }