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.

298 lines
6.2 KiB

4 years ago
  1. package utils
  2. import (
  3. "errors"
  4. "log"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type ITimer struct {
  11. *time.Timer
  12. name string
  13. exited bool
  14. isRunning bool
  15. d time.Duration
  16. fn func()
  17. }
  18. func itimerDefaultFunc() {}
  19. func NewTimer(d time.Duration) *ITimer {
  20. tm := &ITimer{
  21. Timer: time.NewTimer(d),
  22. exited: false,
  23. isRunning: true,
  24. d: d,
  25. fn: itimerDefaultFunc,
  26. }
  27. tm.Pause()
  28. return tm
  29. }
  30. func (tm *ITimer) SetName(nm string) {
  31. tm.name = nm
  32. }
  33. func (tm *ITimer) Name() string {
  34. return tm.name
  35. }
  36. func (tm *ITimer) Exit() bool {
  37. tm.exited = tm.Timer.Reset(0)
  38. return tm.exited
  39. }
  40. func (tm *ITimer) IsExit() bool {
  41. return tm.exited
  42. }
  43. func (tm *ITimer) reset() bool {
  44. if tm.exited {
  45. return false
  46. }
  47. tm.isRunning = true
  48. return tm.Timer.Reset(tm.d)
  49. }
  50. func (tm *ITimer) Pause() bool {
  51. tm.isRunning = false
  52. return tm.Timer.Reset(time.Duration(math.MaxInt64))
  53. }
  54. func (tm *ITimer) Continue() bool {
  55. tm.isRunning = true
  56. return tm.reset()
  57. }
  58. func (tm *ITimer) IsRunning() bool {
  59. if tm.exited {
  60. return false
  61. }
  62. return tm.isRunning
  63. }
  64. func (tm *ITimer) SetFunc(fn func()) {
  65. tm.fn = fn
  66. }
  67. func (tm *ITimer) NowExecFunc() {
  68. if tm.fn != nil {
  69. go tm.fn()
  70. }
  71. tm.reset()
  72. }
  73. func (tm *ITimer) Run() {
  74. for {
  75. select {
  76. case <-tm.C:
  77. if tm.IsExit() {
  78. tm.isRunning = false
  79. return
  80. }
  81. tm.reset()
  82. if tm.fn != nil {
  83. go tm.fn()
  84. }
  85. }
  86. }
  87. }
  88. //format time like java, such as: yyyy-MM-dd HH:mm:ss
  89. func TimeFormat(t time.Time, format string) string {
  90. //year
  91. if strings.ContainsAny(format, "y") {
  92. year := strconv.Itoa(t.Year())
  93. if strings.Count(format, "yy") == 1 && strings.Count(format, "y") == 2 {
  94. format = strings.Replace(format, "yy", year[2:], 1)
  95. } else if strings.Count(format, "yyyy") == 1 && strings.Count(format, "y") == 4 {
  96. format = strings.Replace(format, "yyyy", year, 1)
  97. } else {
  98. log.Fatalln("format year error! please 'yyyy' or 'yy'")
  99. }
  100. }
  101. //month
  102. if strings.ContainsAny(format, "M") {
  103. var month string
  104. if int(t.Month()) < 10 {
  105. month = "0" + strconv.Itoa(int(t.Month()))
  106. } else {
  107. month = strconv.Itoa(int(t.Month()))
  108. }
  109. if strings.Count(format, "MM") == 1 && strings.Count(format, "M") == 2 {
  110. format = strings.Replace(format, "MM", month, 1)
  111. } else {
  112. log.Fatalln("format month error! please 'MM'")
  113. }
  114. }
  115. //day
  116. if strings.ContainsAny(format, "d") {
  117. var day string
  118. if t.Day() < 10 {
  119. day = "0" + strconv.Itoa(t.Day())
  120. } else {
  121. day = strconv.Itoa(t.Day())
  122. }
  123. if strings.Count(format, "dd") == 1 && strings.Count(format, "d") == 2 {
  124. format = strings.Replace(format, "dd", day, 1)
  125. } else {
  126. log.Fatalln("format day error! please 'dd'")
  127. }
  128. }
  129. //hour
  130. if strings.ContainsAny(format, "H") {
  131. var hour string
  132. if t.Hour() < 10 {
  133. hour = "0" + strconv.Itoa(t.Hour())
  134. } else {
  135. hour = strconv.Itoa(t.Hour())
  136. }
  137. if strings.Count(format, "HH") == 1 && strings.Count(format, "H") == 2 {
  138. format = strings.Replace(format, "HH", hour, 1)
  139. } else {
  140. log.Fatalln("format hour error! please 'HH'")
  141. }
  142. }
  143. //minute
  144. if strings.ContainsAny(format, "m") {
  145. var minute string
  146. if t.Minute() < 10 {
  147. minute = "0" + strconv.Itoa(t.Minute())
  148. } else {
  149. minute = strconv.Itoa(t.Minute())
  150. }
  151. if strings.Count(format, "mm") == 1 && strings.Count(format, "m") == 2 {
  152. format = strings.Replace(format, "mm", minute, 1)
  153. } else {
  154. log.Fatalln("format minute error! please 'mm'")
  155. }
  156. }
  157. //second
  158. if strings.ContainsAny(format, "s") {
  159. var second string
  160. if t.Second() < 10 {
  161. second = "0" + strconv.Itoa(t.Second())
  162. } else {
  163. second = strconv.Itoa(t.Second())
  164. }
  165. if strings.Count(format, "ss") == 1 && strings.Count(format, "s") == 2 {
  166. format = strings.Replace(format, "ss", second, 1)
  167. } else {
  168. log.Fatalln("format second error! please 'ss'")
  169. }
  170. }
  171. return format
  172. }
  173. //2007-11-23 10:02:14/20071123100214
  174. func TimeParse(str string) (time.Time, error) {
  175. ll := len(str)
  176. if ll == 19 {
  177. loc, _ := time.LoadLocation("Local")
  178. return time.ParseInLocation("2006-01-02 15:04:05", str, loc)
  179. } else if ll == 14 {
  180. loc, _ := time.LoadLocation("Local")
  181. return time.ParseInLocation("20060102150405", str, loc)
  182. }
  183. return time.Time{}, errors.New("str len is error")
  184. }
  185. //17:02:03/170203
  186. func TimeParseHHmmss(str string) (time.Time, error) {
  187. ll := len(str)
  188. if ll != 8 && ll != 6 {
  189. return time.Time{}, errors.New("input str is error")
  190. }
  191. if ll == 8 {
  192. return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str))
  193. }
  194. return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str))
  195. }
  196. //17:02/1702
  197. func TimeParseHHmm(str string) (time.Time, error) {
  198. ll := len(str)
  199. if ll != 5 && ll != 4 {
  200. return time.Time{}, errors.New("input str is error")
  201. }
  202. if ll == 5 {
  203. return TimeParse(TimeFormat(time.Now(), "yyyy-MM-dd "+str+":00"))
  204. }
  205. return TimeParse(TimeFormat(time.Now(), "yyyyMMdd"+str+"00"))
  206. }
  207. //2007-05-69/20070569
  208. func TimeParseyyyyMMdd(str string) (time.Time, error) {
  209. ll := len(str)
  210. if ll != 10 && ll != 8 {
  211. return time.Time{}, errors.New("input str is error")
  212. }
  213. if ll == 10 {
  214. return TimeParse(TimeFormat(time.Now(), str+" 00:00:00"))
  215. }
  216. return TimeParse(TimeFormat(time.Now(), str+"000000"))
  217. }
  218. //计算时间差返回秒级 样例: T1 = 16:00 t2 = 08:00
  219. func Timelen(t1 string, t2 string) (int, error) {
  220. var time1 int64
  221. var time2 int64
  222. len1 := len(t1)
  223. if len1 != 4 && len1 != 5 {
  224. return 0, errors.New("input str is error")
  225. }
  226. len2 := len(t2)
  227. if len2 != 4 && len2 != 5 {
  228. return 0, errors.New("input str is error")
  229. }
  230. tem1, _ := TimeParseHHmm(t1)
  231. time1 = tem1.Unix()
  232. tem2, _ := TimeParseHHmm(t2)
  233. time2 = tem2.Unix()
  234. timelen := time1 - time2
  235. return int(timelen), nil
  236. }
  237. //获取某一天的0点时间
  238. func GetZeroTime(d time.Time) time.Time {
  239. now := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  240. return now
  241. }
  242. //返回格式:例如2016-05-01 00:00:00的时间戳到2016-06-01 00:00:00的时间戳
  243. func TimeReturnMonth(now time.Time) (firstMonth string, lastMonth string) {
  244. currentYear, currentMonth, _ := now.Date()
  245. currentLocation := now.Location()
  246. firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
  247. lastOfMonth := firstOfMonth.AddDate(0, 1, 0).Add(time.Second * -1)
  248. return TimeFormat(firstOfMonth,"yyyyMMdd"), TimeFormat(lastOfMonth,"yyyyMMdd")
  249. }