劲创营---任务项目
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

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