util.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package script
  2. import (
  3. "fmt"
  4. lua "github.com/yuin/gopher-lua"
  5. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "log"
  7. "regexp"
  8. "time"
  9. )
  10. func Catch() {
  11. if err := recover(); err != nil {
  12. log.Println(err)
  13. }
  14. }
  15. // MapToTable converts a Go map to a lua table
  16. func MapToTable(m map[string]interface{}) *lua.LTable {
  17. // Main table pointer
  18. resultTable := &lua.LTable{}
  19. // Loop map
  20. for key, element := range m {
  21. switch element.(type) {
  22. case float64:
  23. resultTable.RawSetString(key, lua.LNumber(element.(float64)))
  24. case int64:
  25. resultTable.RawSetString(key, lua.LNumber(element.(int64)))
  26. case string:
  27. resultTable.RawSetString(key, lua.LString(element.(string)))
  28. case bool:
  29. resultTable.RawSetString(key, lua.LBool(element.(bool)))
  30. case []byte:
  31. resultTable.RawSetString(key, lua.LString(string(element.([]byte))))
  32. case map[string]interface{}:
  33. // Get table from map
  34. tble := MapToTable(element.(map[string]interface{}))
  35. resultTable.RawSetString(key, tble)
  36. case time.Time:
  37. resultTable.RawSetString(key, lua.LNumber(element.(time.Time).Unix()))
  38. case []map[string]interface{}:
  39. // Create slice table
  40. sliceTable := &lua.LTable{}
  41. // Loop element
  42. for _, s := range element.([]map[string]interface{}) {
  43. // Get table from map
  44. tble := MapToTable(s)
  45. sliceTable.Append(tble)
  46. }
  47. // Set slice table
  48. resultTable.RawSetString(key, sliceTable)
  49. case []interface{}:
  50. // Create slice table
  51. sliceTable := &lua.LTable{}
  52. // Loop interface slice
  53. for _, s := range element.([]interface{}) {
  54. // Switch interface type
  55. switch s.(type) {
  56. case map[string]interface{}:
  57. // Convert map to table
  58. t := MapToTable(s.(map[string]interface{}))
  59. // Append result
  60. sliceTable.Append(t)
  61. case float64:
  62. // Append result as number
  63. sliceTable.Append(lua.LNumber(s.(float64)))
  64. case string:
  65. // Append result as string
  66. sliceTable.Append(lua.LString(s.(string)))
  67. case bool:
  68. // Append result as bool
  69. sliceTable.Append(lua.LBool(s.(bool)))
  70. }
  71. }
  72. // Append to main table
  73. resultTable.RawSetString(key, sliceTable)
  74. }
  75. }
  76. return resultTable
  77. }
  78. // TabletoMap converts a lua table to go map
  79. func TableToMap(t *lua.LTable) map[string]interface{} {
  80. ret := make(map[string]interface{})
  81. t.ForEach(func(k, v lua.LValue) {
  82. key := fmt.Sprint(k)
  83. if val, ok := v.(*lua.LTable); ok {
  84. ret[key] = TableToMap(val)
  85. } else {
  86. if val, ok := v.(lua.LString); ok {
  87. ret[key] = string(val)
  88. } else if val, ok := v.(lua.LNumber); ok {
  89. ret[key] = int64(val)
  90. } else if val, ok := v.(lua.LBool); ok {
  91. ret[key] = bool(val)
  92. } else {
  93. ret[key] = fmt.Sprint(v)
  94. }
  95. }
  96. })
  97. return ret
  98. }
  99. func getPublitime(text string) string {
  100. //取数字年月日时分秒
  101. re := regexp.MustCompile(`(\d+)`)
  102. matches := re.FindAllString(text, -1)
  103. if len(matches) == 1 { //20240927 2024927 2024 927
  104. return "0"
  105. }
  106. if len(matches) < 2 { //最少有年月 或月日
  107. return "0"
  108. }
  109. y := padDigital(matches[0]) //年
  110. m := padDigital(matches[1]) // 月
  111. if len(matches) == 2 { //年月或月日
  112. if dateFormatCorret("year", y) && dateFormatCorret("month", m) { //年月
  113. return formatTime(y, m, "", "", "", "")
  114. } else if dateFormatCorret("month", y) && dateFormatCorret("day", m) { //月日
  115. return formatTime("", y, m, "", "", "")
  116. }
  117. } else if len(matches) >= 3 {
  118. d := padDigital(matches[2])
  119. if len(matches) == 3 { //年月日(无月日时,时分秒的情况)
  120. if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) {
  121. return formatTime(y, m, d, "", "", "")
  122. }
  123. } else if len(matches) >= 5 { //年月日时分
  124. h := padDigital(matches[3])
  125. mm := padDigital(matches[4])
  126. if len(matches) == 5 { //年月日时分
  127. if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) && dateFormatCorret("hour", h) && dateFormatCorret("minute", mm) {
  128. return formatTime(y, m, d, h, mm, "")
  129. }
  130. } else if len(matches) == 6 { //年月日时分秒
  131. s := padDigital(matches[5])
  132. if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) && dateFormatCorret("hour", h) && dateFormatCorret("minute", mm) && dateFormatCorret("second", s) {
  133. return formatTime(y, m, d, h, mm, s)
  134. }
  135. }
  136. }
  137. }
  138. return "0"
  139. }
  140. func dateFormatCorret(stype, date string) bool {
  141. switch stype {
  142. case "year":
  143. return len(date) == 4
  144. case "month":
  145. return len(date) <= 2 && date <= "12" && date >= "01"
  146. case "day":
  147. return len(date) <= 2 && date <= "31" && date >= "01"
  148. case "hour":
  149. return len(date) <= 2 && date <= "24" && date >= "00"
  150. case "minute":
  151. return len(date) <= 2 && date <= "59" && date >= "00"
  152. case "second":
  153. return len(date) <= 2 && date <= "59" && date >= "00"
  154. }
  155. return false
  156. }
  157. func padDigital(text string) string {
  158. if len(text) < 2 {
  159. return "0" + text
  160. }
  161. return text
  162. }
  163. func formatTime(year, month, day, hour, minute, second string) string {
  164. now := time.Now()
  165. if year == "" {
  166. year = fmt.Sprint(now.Year())
  167. } else if month == "" {
  168. month = fmt.Sprint(now.Month())
  169. } else if day == "" {
  170. day = fmt.Sprint(now.Day())
  171. } else if hour == "" {
  172. hour = fmt.Sprint(now.Hour())
  173. } else if minute == "" {
  174. minute = fmt.Sprint(now.Minute())
  175. } else if second == "" {
  176. second = fmt.Sprint(now.Second())
  177. }
  178. return fmt.Sprintf("%4d-%02d-%02d %02d:%02d:%02d",
  179. qu.IntAll(year),
  180. qu.IntAll(month),
  181. qu.IntAll(day),
  182. qu.IntAll(hour),
  183. qu.IntAll(minute),
  184. qu.IntAll(second),
  185. )
  186. }