totimestamp.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // totimestamp
  2. package clear
  3. import (
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. var reg, regA, regB, regC, regD, regAfter *regexp.Regexp
  11. const (
  12. T = 365 * 86400
  13. )
  14. var item = map[string]string{
  15. "一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
  16. "六": "6", "七": "7", "九": "9", "十": "10", "零": "0", "〇": "0",
  17. "1": "1", "2": "2", "3": "3", "4": "4", "5": "5",
  18. "6": "6", "7": "7", "8": "8", "9": "9", "0": "0",
  19. }
  20. func init() {
  21. //二〇一五年十一月四日十五时
  22. reg, _ = regexp.Compile(`\d+`)
  23. regA, _ = regexp.Compile(`[一|二|三|四|五|六|七|八|九|十|零|〇|1|2|3|4|5|6|7|8|9|0]`)
  24. regB, _ = regexp.Compile(`\d+年\d+月\d+日((上|下)午)?\s*\d+[::时]\d+分?[-—]\d+[::时]\d+时?分?`)
  25. regC, _ = regexp.Compile(`\s*\d+[::时]\d+分?[-—]`)
  26. regD, _ = regexp.Compile(`([一|二|三|四|五|六|七|八|九|十|零|〇]{4})年([一|二|三|四|五|六|七|八|九|十]{1,2})月([一|二|三|四|五|六|七|八|九|十]{1,3})日([一|二|三|四|五|六|七|八|九|十]{1,3})时`)
  27. regAfter, _ = regexp.Compile(`(下午D?\d{1,2}[时|:|:|h|H])`)
  28. }
  29. /*字符时间转时间戳
  30. 支持全角
  31. 20060102->时间戳
  32. 20060102150405->时间戳
  33. 01%02->时间戳
  34. 2006%01%02->时间戳
  35. 2006%01%02%15->时间戳
  36. 2006%01%02%15%04->时间戳
  37. 2006%01%02%15%04%05->时间戳
  38. */
  39. func ObjToTimestamp(data []interface{},spidercode ...string) []interface{} {
  40. tmp := fmt.Sprint(data[0])
  41. //处理类似:二〇一五年十一月四日十五时
  42. cht := regD.FindStringSubmatch(tmp)
  43. if len(cht) == 5 {
  44. y := chineseToNumber(cht[1])
  45. m := 0
  46. for _, v := range []rune(cht[2]) {
  47. it, _ := strconv.Atoi(item[string(v)])
  48. m += it
  49. }
  50. d := 0
  51. for _, v := range []rune(cht[3]) {
  52. it, _ := strconv.Atoi(item[string(v)])
  53. d += it
  54. }
  55. M := 0
  56. for _, v := range []rune(cht[4]) {
  57. it, _ := strconv.Atoi(item[string(v)])
  58. M += it
  59. }
  60. tmp = fmt.Sprintf("%s年%d月%d日%d时", y, m, d, M)
  61. }
  62. //2016年12月7日上午9:00-11:30时 时间范围处理 取后面的时间
  63. if regB.MatchString(tmp) {
  64. tmp = regC.ReplaceAllString(tmp, "")
  65. }
  66. //2017年11月13日下午3时30分
  67. addreptime := int64(0)
  68. if regAfter.MatchString(tmp) {
  69. addreptime = 12 * 60 * 60
  70. }
  71. regRepl, _ := regexp.Compile(`[,,]`)
  72. tmp = regRepl.ReplaceAllString(tmp, "")
  73. for _, v := range spaces {
  74. strings.Replace(tmp, v, " ", -1)
  75. }
  76. tmps := reg.FindAllString(chineseToNumber(tmp), -1)
  77. //处理类似2016-12-0909:30:00时间
  78. if len(tmps) > 2 && len(tmps[2]) > 2 {
  79. newtmp := []string{}
  80. for k, v := range tmps {
  81. if k == 2 {
  82. newtmp = append(newtmp, v[0:2], v[2:])
  83. } else {
  84. newtmp = append(newtmp, v)
  85. }
  86. }
  87. tmps = newtmp
  88. }
  89. timestr := "" //2006-01-02 15:04:05
  90. timestamp := int64(0)
  91. if len(tmps) == 1 {
  92. if len(tmps[0]) == 8 {
  93. timestr = tmps[0][0:4] + "-" + tmps[0][4:6] + "-" + tmps[0][6:8]
  94. t, _ := time.ParseInLocation("2006-01-02-15-04", timestr+"-09-00", time.Local)
  95. timestamp = t.Unix()
  96. } else if len(tmps[0]) == 14 {
  97. timestr = tmps[0][0:4] + "-" + tmps[0][4:6] + "-" + tmps[0][6:8] + " " + tmps[0][8:10] + ":" + tmps[0][10:12] + ":" + tmps[0][12:14]
  98. t, _ := time.ParseInLocation("2006-01-02 15:04:00", timestr, time.Local)
  99. timestamp = t.Unix()
  100. }
  101. } else if len(tmps) == 2 {
  102. timestr = fmt.Sprint(time.Now().Year()) + "-" + MDhmsRepair(tmps[0]) + "-" + MDhmsRepair(tmps[1])
  103. t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
  104. timestamp = t.Unix()
  105. } else if len(tmps) == 3 {
  106. timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2])
  107. t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
  108. timestamp = t.Unix()
  109. } else if len(tmps) == 4 {
  110. timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3])
  111. t, _ := time.ParseInLocation("2006-01-02 15", timestr, time.Local)
  112. timestamp = t.Unix()
  113. } else if len(tmps) >= 5 {
  114. timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3]) + ":" + MDhmsRepair(tmps[4])
  115. t, _ := time.ParseInLocation("2006-01-02 15:04", timestr, time.Local)
  116. timestamp = t.Unix()
  117. }
  118. if timestamp <= 0 || timestamp > (time.Now().Unix()+T) {
  119. data[0] = ""
  120. } else {
  121. if addreptime > 0 {
  122. timestamp += addreptime
  123. }
  124. data[0] = timestamp
  125. }
  126. return data
  127. }
  128. //补位
  129. func MDhmsRepair(t string) string {
  130. if len(t) == 1 {
  131. return "0" + t
  132. } else {
  133. return t
  134. }
  135. }
  136. //汉子数和全角转数字
  137. func chineseToNumber(con string) string {
  138. tmp := regA.ReplaceAllStringFunc(con, func(key string) string {
  139. if item[key] != "" {
  140. return item[key]
  141. } else {
  142. return key
  143. }
  144. return key
  145. })
  146. return tmp
  147. }