蛋蛋星球-制度模式
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.9 KiB

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