蛋蛋星球 后台端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

271 рядки
8.0 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func StrToTime(s string) (int64, error) {
  11. // delete all not int characters
  12. if s == "" {
  13. return time.Now().Unix(), nil
  14. }
  15. r := make([]rune, 14)
  16. l := 0
  17. // 过滤除数字以外的字符
  18. for _, v := range s {
  19. if '0' <= v && v <= '9' {
  20. r[l] = v
  21. l++
  22. if l == 14 {
  23. break
  24. }
  25. }
  26. }
  27. for l < 14 {
  28. r[l] = '0' // 补0
  29. l++
  30. }
  31. t, err := time.Parse("20060102150405", string(r))
  32. if err != nil {
  33. return 0, err
  34. }
  35. return t.Unix(), nil
  36. }
  37. func TimeToStr(unixSecTime interface{}, layout ...string) string {
  38. i := AnyToInt64(unixSecTime)
  39. if i == 0 {
  40. return ""
  41. }
  42. f := "2006-01-02 15:04:05"
  43. if len(layout) > 0 {
  44. f = layout[0]
  45. }
  46. return time.Unix(i, 0).Format(f)
  47. }
  48. func Time2String(date time.Time, format string) string {
  49. if format == "" {
  50. format = "2006-01-02 15:04:05"
  51. }
  52. timeS := date.Format(format)
  53. if timeS == "0001-01-01 00:00:00" {
  54. return ""
  55. }
  56. return timeS
  57. }
  58. func FormatNanoUnix() string {
  59. return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1)
  60. }
  61. func TimeParse(format, src string) (time.Time, error) {
  62. return time.ParseInLocation(format, src, time.Local)
  63. }
  64. func TimeParseStd(src string) time.Time {
  65. t, _ := TimeParse("2006-01-02 15:04:05", src)
  66. return t
  67. }
  68. func TimeParseDateStd(src string) time.Time {
  69. t, _ := TimeParse("2006-01-02", src)
  70. return t
  71. }
  72. func TimeStdParseUnix(src string) int64 {
  73. t, err := TimeParse("2006-01-02 15:04:05", src)
  74. if err != nil {
  75. return 0
  76. }
  77. return t.Unix()
  78. }
  79. // 获取一个当前时间 时间间隔 时间戳
  80. func GetTimeInterval(unit string, amount int) (startTime, endTime int64) {
  81. t := time.Now()
  82. nowTime := t.Unix()
  83. tmpTime := int64(0)
  84. switch unit {
  85. case "years":
  86. tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  87. case "months":
  88. tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  89. case "days":
  90. tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix()
  91. case "hours":
  92. tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix()
  93. }
  94. if amount > 0 {
  95. startTime = nowTime
  96. endTime = tmpTime
  97. } else {
  98. startTime = tmpTime
  99. endTime = nowTime
  100. }
  101. return
  102. }
  103. // 几天前
  104. func TimeInterval(newTime int) string {
  105. now := time.Now().Unix()
  106. newTime64 := AnyToInt64(newTime)
  107. if newTime64 >= now {
  108. return "刚刚"
  109. }
  110. interval := now - newTime64
  111. switch {
  112. case interval < 60:
  113. return AnyToString(interval) + "秒前"
  114. case interval < 60*60:
  115. return AnyToString(interval/60) + "分前"
  116. case interval < 60*60*24:
  117. return AnyToString(interval/60/60) + "小时前"
  118. case interval < 60*60*24*30:
  119. return AnyToString(interval/60/60/24) + "天前"
  120. case interval < 60*60*24*30*12:
  121. return AnyToString(interval/60/60/24/30) + "月前"
  122. default:
  123. return AnyToString(interval/60/60/24/30/12) + "年前"
  124. }
  125. }
  126. // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10
  127. func HmsToUnix(str string) (int64, error) {
  128. t := time.Now()
  129. arr := strings.Split(str, ":")
  130. if len(arr) < 2 {
  131. return 0, errors.New("Time format error")
  132. }
  133. h, _ := strconv.Atoi(arr[0])
  134. m, _ := strconv.Atoi(arr[1])
  135. s := 0
  136. if len(arr) == 3 {
  137. s, _ = strconv.Atoi(arr[3])
  138. }
  139. formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s)
  140. res, err := time.ParseInLocation("20060102150405", formatted1, time.Local)
  141. if err != nil {
  142. return 0, err
  143. } else {
  144. return res.Unix(), nil
  145. }
  146. }
  147. // 获取特定时间范围
  148. func GetTimeRange(s string) map[string]int64 {
  149. t := time.Now()
  150. var stime, etime time.Time
  151. switch s {
  152. case "today":
  153. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  154. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  155. case "yesterday":
  156. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  157. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  158. case "within_seven_days":
  159. // 前6天0点
  160. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  161. // 明天 0点
  162. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  163. case "current_month":
  164. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  165. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  166. case "last_month":
  167. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  168. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  169. }
  170. return map[string]int64{
  171. "start": stime.Unix(),
  172. "end": etime.Unix(),
  173. }
  174. }
  175. // 获取特定时间范围
  176. func GetDateTimeRangeStr(s string) (string, string) {
  177. t := time.Now()
  178. var stime, etime time.Time
  179. switch s {
  180. case "today":
  181. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  182. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  183. case "yesterday":
  184. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  185. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  186. case "within_seven_days":
  187. // 前6天0点
  188. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  189. // 明天 0点
  190. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  191. case "current_month":
  192. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  193. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  194. case "last_month":
  195. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  196. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  197. }
  198. return stime.Format("2006-01-02 15:04:05"), etime.Format("2006-01-02 15:04:05")
  199. }
  200. // 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  201. func GetFirstDateOfMonth(d time.Time) time.Time {
  202. d = d.AddDate(0, 0, -d.Day()+1)
  203. return GetZeroTime(d)
  204. }
  205. // 获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  206. func GetLastDateOfMonth(d time.Time) time.Time {
  207. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  208. }
  209. // 获取某一天的0点时间
  210. func GetZeroTime(d time.Time) time.Time {
  211. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  212. }
  213. // GetWeekInfo 获取周数、开始时间、结束时间
  214. func GetWeekInfo(dateStr string) (string, string, string, string) {
  215. date := TimeParseStd(dateStr)
  216. year, week := date.ISOWeek()
  217. location, _ := time.LoadLocation("Asia/Shanghai")
  218. // 计算给定年份1月1日是星期几
  219. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  220. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  221. // 计算给定年份的第一周的开始日期
  222. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  223. // 计算给定周的开始日期
  224. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  225. weekEnd := weekStart.AddDate(0, 0, 6)
  226. return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  227. }
  228. // GetWeekInfoByDate 获取周数、开始时间、结束时间
  229. func GetWeekInfoByDate(dateStr string) (string, string, string, string) {
  230. date := TimeParseDateStd(dateStr)
  231. year, week := date.ISOWeek()
  232. location, _ := time.LoadLocation("Asia/Shanghai")
  233. // 计算给定年份1月1日是星期几
  234. startOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, location)
  235. daysOffset := int(startOfYear.Weekday()) - int(time.Monday) + 1
  236. // 计算给定年份的第一周的开始日期
  237. firstWeekStart := startOfYear.AddDate(0, 0, -daysOffset+1)
  238. // 计算给定周的开始日期
  239. weekStart := firstWeekStart.AddDate(0, 0, (week-1)*7)
  240. weekEnd := weekStart.AddDate(0, 0, 6)
  241. return IntToStr(year), IntToStr(week), weekStart.Format("2006-01-02"), weekEnd.Format("2006-01-02")
  242. }
  243. func IsTimeFormat(s string) bool {
  244. // 正则表达式匹配HH:MM格式
  245. re := regexp.MustCompile(`^(\d{2}):(\d{2})$`)
  246. return re.MatchString(s)
  247. }