面包店
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 6.3 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 Time2String(date time.Time, format string) string {
  48. if format == "" {
  49. format = "2006-01-02 15:04:05"
  50. }
  51. timeS := date.Format(format)
  52. if timeS == "0001-01-01 00:00:00" {
  53. return ""
  54. }
  55. return timeS
  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 TimeParseDateStd(src string) time.Time {
  68. t, _ := TimeParse("2006-01-02", src)
  69. return t
  70. }
  71. func TimeStdParseUnix(src string) int64 {
  72. t, err := TimeParse("2006-01-02 15:04:05", src)
  73. if err != nil {
  74. return 0
  75. }
  76. return t.Unix()
  77. }
  78. // 获取一个当前时间 时间间隔 时间戳
  79. func GetTimeInterval(unit string, amount int) (startTime, endTime int64) {
  80. t := time.Now()
  81. nowTime := t.Unix()
  82. tmpTime := int64(0)
  83. switch unit {
  84. case "years":
  85. tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  86. case "months":
  87. tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix()
  88. case "days":
  89. tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix()
  90. case "hours":
  91. tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix()
  92. }
  93. if amount > 0 {
  94. startTime = nowTime
  95. endTime = tmpTime
  96. } else {
  97. startTime = tmpTime
  98. endTime = nowTime
  99. }
  100. return
  101. }
  102. // 几天前
  103. func TimeInterval(newTime int) string {
  104. now := time.Now().Unix()
  105. newTime64 := AnyToInt64(newTime)
  106. if newTime64 >= now {
  107. return "刚刚"
  108. }
  109. interval := now - newTime64
  110. switch {
  111. case interval < 60:
  112. return AnyToString(interval) + "秒前"
  113. case interval < 60*60:
  114. return AnyToString(interval/60) + "分前"
  115. case interval < 60*60*24:
  116. return AnyToString(interval/60/60) + "小时前"
  117. case interval < 60*60*24*30:
  118. return AnyToString(interval/60/60/24) + "天前"
  119. case interval < 60*60*24*30*12:
  120. return AnyToString(interval/60/60/24/30) + "月前"
  121. default:
  122. return AnyToString(interval/60/60/24/30/12) + "年前"
  123. }
  124. }
  125. // 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10
  126. func HmsToUnix(str string) (int64, error) {
  127. t := time.Now()
  128. arr := strings.Split(str, ":")
  129. if len(arr) < 2 {
  130. return 0, errors.New("Time format error")
  131. }
  132. h, _ := strconv.Atoi(arr[0])
  133. m, _ := strconv.Atoi(arr[1])
  134. s := 0
  135. if len(arr) == 3 {
  136. s, _ = strconv.Atoi(arr[3])
  137. }
  138. formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s)
  139. res, err := time.ParseInLocation("20060102150405", formatted1, time.Local)
  140. if err != nil {
  141. return 0, err
  142. } else {
  143. return res.Unix(), nil
  144. }
  145. }
  146. // 获取特定时间范围
  147. func GetTimeRange(s string) map[string]int64 {
  148. t := time.Now()
  149. var stime, etime time.Time
  150. switch s {
  151. case "today":
  152. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  153. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  154. case "yesterday":
  155. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  156. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  157. case "within_seven_days":
  158. // 前6天0点
  159. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  160. // 明天 0点
  161. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  162. case "current_month":
  163. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  164. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  165. case "last_month":
  166. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  167. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  168. }
  169. return map[string]int64{
  170. "start": stime.Unix(),
  171. "end": etime.Unix(),
  172. }
  173. }
  174. // 获取特定时间范围
  175. func GetDateTimeRangeStr(s string) (string, string) {
  176. t := time.Now()
  177. var stime, etime time.Time
  178. switch s {
  179. case "today":
  180. stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  181. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  182. case "yesterday":
  183. stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location())
  184. etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  185. case "within_seven_days":
  186. // 前6天0点
  187. stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location())
  188. // 明天 0点
  189. etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location())
  190. case "current_month":
  191. stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  192. etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location())
  193. case "last_month":
  194. stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location())
  195. etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
  196. }
  197. return stime.Format("2006-01-02 15:04:05"), etime.Format("2006-01-02 15:04:05")
  198. }
  199. // 获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
  200. func GetFirstDateOfMonth(d time.Time) time.Time {
  201. d = d.AddDate(0, 0, -d.Day()+1)
  202. return GetZeroTime(d)
  203. }
  204. // 获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
  205. func GetLastDateOfMonth(d time.Time) time.Time {
  206. return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
  207. }
  208. // 获取某一天的0点时间
  209. func GetZeroTime(d time.Time) time.Time {
  210. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  211. }