utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package util
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/up/util/timerqueue"
  7. "go-common/library/log"
  8. "go-common/library/net/http/blademaster"
  9. xtime "go-common/library/time"
  10. )
  11. var (
  12. //GlobalTimer timer queue
  13. GlobalTimer = timerqueue.New()
  14. )
  15. //ParseCommonTime parse to library common time
  16. func ParseCommonTime(layout string, value string) (t xtime.Time, err error) {
  17. date, e := time.ParseInLocation(layout, value, time.Local)
  18. err = e
  19. if err == nil {
  20. t = xtime.Time(date.Unix())
  21. }
  22. return
  23. }
  24. //Unique unique the slice
  25. func Unique(intSlice []int64) []int64 {
  26. keys := make(map[int64]bool)
  27. var list []int64
  28. for _, entry := range intSlice {
  29. if _, value := keys[entry]; !value {
  30. keys[entry] = true
  31. list = append(list, entry)
  32. }
  33. }
  34. return list
  35. }
  36. //GetContextValueInt64 get context int64
  37. func GetContextValueInt64(c *blademaster.Context, key string) (v int64, ok bool) {
  38. var vtemp, o = c.Get(key)
  39. ok = o
  40. if ok {
  41. v, _ = vtemp.(int64)
  42. }
  43. return
  44. }
  45. //GetContextValueString get context string
  46. func GetContextValueString(c *blademaster.Context, key string) (v string, ok bool) {
  47. var vtemp, o = c.Get(key)
  48. ok = o
  49. if ok {
  50. v, _ = vtemp.(string)
  51. }
  52. return
  53. }
  54. const (
  55. trimSet = "\r\n "
  56. )
  57. //ExplodeInt64 explode string to slice
  58. func ExplodeInt64(str string, seperator string) (result []int64) {
  59. var strMids = strings.Split(str, seperator)
  60. for _, v := range strMids {
  61. mid, e := strconv.ParseInt(strings.Trim(v, trimSet), 10, 64)
  62. if e != nil {
  63. continue
  64. }
  65. result = append(result, mid)
  66. }
  67. return
  68. }
  69. //ExplodeUint32 explode string to slice
  70. func ExplodeUint32(str string, seperator string) (result []uint32) {
  71. var strMids = strings.Split(str, seperator)
  72. for _, v := range strMids {
  73. mid, e := strconv.ParseInt(strings.Trim(v, trimSet), 10, 64)
  74. if e != nil {
  75. continue
  76. }
  77. result = append(result, uint32(mid))
  78. }
  79. return
  80. }
  81. //GetNextPeriodTime get next period time from current time
  82. // clock, like "03:05:00"
  83. // period, like 24h
  84. // currentTime, like now
  85. // return the next alarm time for this clock
  86. func GetNextPeriodTime(clock string, period time.Duration, currentTime time.Time) (next time.Time, err error) {
  87. var now = currentTime
  88. var startTime, e = time.Parse("15:04:05", clock)
  89. err = e
  90. if err != nil {
  91. log.Error("clock is not right, config=%s, should like '12:00:00'")
  92. startTime = time.Date(2000, 1, 1, 3, 0, 0, 0, now.Location())
  93. }
  94. next = time.Date(now.Year(), now.Month(), now.Day(), startTime.Hour(), startTime.Minute(), startTime.Second(), 0, now.Location())
  95. d := next.Sub(now)
  96. for d < 0 {
  97. next = next.Add(period)
  98. d = next.Sub(now)
  99. }
  100. for d > period {
  101. next = next.Add(-period)
  102. d = next.Sub(now)
  103. }
  104. return
  105. }
  106. //TruncateDate 截取到整天,舍去时分秒
  107. func TruncateDate(tm time.Time) time.Time {
  108. var y, m, d = tm.Date()
  109. return time.Date(y, m, d, 0, 0, 0, 0, tm.Location())
  110. }