第三方api接口
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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