蛋蛋星球-客户端
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.

time.go 7.6 KiB

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