123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package script
- import (
- "fmt"
- lua "github.com/yuin/gopher-lua"
- qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
- "log"
- "regexp"
- "time"
- )
- func Catch() {
- if err := recover(); err != nil {
- log.Println(err)
- }
- }
- // MapToTable converts a Go map to a lua table
- func MapToTable(m map[string]interface{}) *lua.LTable {
- // Main table pointer
- resultTable := &lua.LTable{}
- // Loop map
- for key, element := range m {
- switch element.(type) {
- case float64:
- resultTable.RawSetString(key, lua.LNumber(element.(float64)))
- case int64:
- resultTable.RawSetString(key, lua.LNumber(element.(int64)))
- case string:
- resultTable.RawSetString(key, lua.LString(element.(string)))
- case bool:
- resultTable.RawSetString(key, lua.LBool(element.(bool)))
- case []byte:
- resultTable.RawSetString(key, lua.LString(string(element.([]byte))))
- case map[string]interface{}:
- // Get table from map
- tble := MapToTable(element.(map[string]interface{}))
- resultTable.RawSetString(key, tble)
- case time.Time:
- resultTable.RawSetString(key, lua.LNumber(element.(time.Time).Unix()))
- case []map[string]interface{}:
- // Create slice table
- sliceTable := &lua.LTable{}
- // Loop element
- for _, s := range element.([]map[string]interface{}) {
- // Get table from map
- tble := MapToTable(s)
- sliceTable.Append(tble)
- }
- // Set slice table
- resultTable.RawSetString(key, sliceTable)
- case []interface{}:
- // Create slice table
- sliceTable := &lua.LTable{}
- // Loop interface slice
- for _, s := range element.([]interface{}) {
- // Switch interface type
- switch s.(type) {
- case map[string]interface{}:
- // Convert map to table
- t := MapToTable(s.(map[string]interface{}))
- // Append result
- sliceTable.Append(t)
- case float64:
- // Append result as number
- sliceTable.Append(lua.LNumber(s.(float64)))
- case string:
- // Append result as string
- sliceTable.Append(lua.LString(s.(string)))
- case bool:
- // Append result as bool
- sliceTable.Append(lua.LBool(s.(bool)))
- }
- }
- // Append to main table
- resultTable.RawSetString(key, sliceTable)
- }
- }
- return resultTable
- }
- // TabletoMap converts a lua table to go map
- func TableToMap(t *lua.LTable) map[string]interface{} {
- ret := make(map[string]interface{})
- t.ForEach(func(k, v lua.LValue) {
- key := fmt.Sprint(k)
- if val, ok := v.(*lua.LTable); ok {
- ret[key] = TableToMap(val)
- } else {
- if val, ok := v.(lua.LString); ok {
- ret[key] = string(val)
- } else if val, ok := v.(lua.LNumber); ok {
- ret[key] = int64(val)
- } else if val, ok := v.(lua.LBool); ok {
- ret[key] = bool(val)
- } else {
- ret[key] = fmt.Sprint(v)
- }
- }
- })
- return ret
- }
- func getPublitime(text string) string {
- //取数字年月日时分秒
- re := regexp.MustCompile(`(\d+)`)
- matches := re.FindAllString(text, -1)
- if len(matches) == 1 { //20240927 2024927 2024 927
- return "0"
- }
- if len(matches) < 2 { //最少有年月 或月日
- return "0"
- }
- y := padDigital(matches[0]) //年
- m := padDigital(matches[1]) // 月
- if len(matches) == 2 { //年月或月日
- if dateFormatCorret("year", y) && dateFormatCorret("month", m) { //年月
- return formatTime(y, m, "", "", "", "")
- } else if dateFormatCorret("month", y) && dateFormatCorret("day", m) { //月日
- return formatTime("", y, m, "", "", "")
- }
- } else if len(matches) >= 3 {
- d := padDigital(matches[2])
- if len(matches) == 3 { //年月日(无月日时,时分秒的情况)
- if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) {
- return formatTime(y, m, d, "", "", "")
- }
- } else if len(matches) >= 5 { //年月日时分
- h := padDigital(matches[3])
- mm := padDigital(matches[4])
- if len(matches) == 5 { //年月日时分
- if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) && dateFormatCorret("hour", h) && dateFormatCorret("minute", mm) {
- return formatTime(y, m, d, h, mm, "")
- }
- } else if len(matches) == 6 { //年月日时分秒
- s := padDigital(matches[5])
- if dateFormatCorret("year", y) && dateFormatCorret("month", m) && dateFormatCorret("day", d) && dateFormatCorret("hour", h) && dateFormatCorret("minute", mm) && dateFormatCorret("second", s) {
- return formatTime(y, m, d, h, mm, s)
- }
- }
- }
- }
- return "0"
- }
- func dateFormatCorret(stype, date string) bool {
- switch stype {
- case "year":
- return len(date) == 4
- case "month":
- return len(date) <= 2 && date <= "12" && date >= "01"
- case "day":
- return len(date) <= 2 && date <= "31" && date >= "01"
- case "hour":
- return len(date) <= 2 && date <= "24" && date >= "00"
- case "minute":
- return len(date) <= 2 && date <= "59" && date >= "00"
- case "second":
- return len(date) <= 2 && date <= "59" && date >= "00"
- }
- return false
- }
- func padDigital(text string) string {
- if len(text) < 2 {
- return "0" + text
- }
- return text
- }
- func formatTime(year, month, day, hour, minute, second string) string {
- now := time.Now()
- if year == "" {
- year = fmt.Sprint(now.Year())
- } else if month == "" {
- month = fmt.Sprint(now.Month())
- } else if day == "" {
- day = fmt.Sprint(now.Day())
- } else if hour == "" {
- hour = fmt.Sprint(now.Hour())
- } else if minute == "" {
- minute = fmt.Sprint(now.Minute())
- } else if second == "" {
- second = fmt.Sprint(now.Second())
- }
- return fmt.Sprintf("%4d-%02d-%02d %02d:%02d:%02d",
- qu.IntAll(year),
- qu.IntAll(month),
- qu.IntAll(day),
- qu.IntAll(hour),
- qu.IntAll(minute),
- qu.IntAll(second),
- )
- }
|