123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // totimestamp
- package clear
- import (
- "fmt"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- var reg, regA, regB, regC, regD, regAfter *regexp.Regexp
- const (
- T = 365 * 86400
- )
- var item = map[string]string{
- "一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
- "六": "6", "七": "7", "九": "9", "十": "10", "零": "0", "〇": "0",
- "1": "1", "2": "2", "3": "3", "4": "4", "5": "5",
- "6": "6", "7": "7", "8": "8", "9": "9", "0": "0",
- }
- func init() {
- //二〇一五年十一月四日十五时
- reg, _ = regexp.Compile(`\d+`)
- regA, _ = regexp.Compile(`[一|二|三|四|五|六|七|八|九|十|零|〇|1|2|3|4|5|6|7|8|9|0]`)
- regB, _ = regexp.Compile(`\d+年\d+月\d+日((上|下)午)?\s*\d+[::时]\d+分?[-—]\d+[::时]\d+时?分?`)
- regC, _ = regexp.Compile(`\s*\d+[::时]\d+分?[-—]`)
- regD, _ = regexp.Compile(`([一|二|三|四|五|六|七|八|九|十|零|〇]{4})年([一|二|三|四|五|六|七|八|九|十]{1,2})月([一|二|三|四|五|六|七|八|九|十]{1,3})日([一|二|三|四|五|六|七|八|九|十]{1,3})时`)
- regAfter, _ = regexp.Compile(`(下午D?\d{1,2}[时|:|:|h|H])`)
- }
- /*字符时间转时间戳
- 支持全角
- 20060102->时间戳
- 20060102150405->时间戳
- 01%02->时间戳
- 2006%01%02->时间戳
- 2006%01%02%15->时间戳
- 2006%01%02%15%04->时间戳
- 2006%01%02%15%04%05->时间戳
- */
- func ObjToTimestamp(data []interface{},spidercode ...string) []interface{} {
- tmp := fmt.Sprint(data[0])
- //处理类似:二〇一五年十一月四日十五时
- cht := regD.FindStringSubmatch(tmp)
- if len(cht) == 5 {
- y := chineseToNumber(cht[1])
- m := 0
- for _, v := range []rune(cht[2]) {
- it, _ := strconv.Atoi(item[string(v)])
- m += it
- }
- d := 0
- for _, v := range []rune(cht[3]) {
- it, _ := strconv.Atoi(item[string(v)])
- d += it
- }
- M := 0
- for _, v := range []rune(cht[4]) {
- it, _ := strconv.Atoi(item[string(v)])
- M += it
- }
- tmp = fmt.Sprintf("%s年%d月%d日%d时", y, m, d, M)
- }
- //2016年12月7日上午9:00-11:30时 时间范围处理 取后面的时间
- if regB.MatchString(tmp) {
- tmp = regC.ReplaceAllString(tmp, "")
- }
- //2017年11月13日下午3时30分
- addreptime := int64(0)
- if regAfter.MatchString(tmp) {
- addreptime = 12 * 60 * 60
- }
- regRepl, _ := regexp.Compile(`[,,]`)
- tmp = regRepl.ReplaceAllString(tmp, "")
- for _, v := range spaces {
- strings.Replace(tmp, v, " ", -1)
- }
- tmps := reg.FindAllString(chineseToNumber(tmp), -1)
- //处理类似2016-12-0909:30:00时间
- if len(tmps) > 2 && len(tmps[2]) > 2 {
- newtmp := []string{}
- for k, v := range tmps {
- if k == 2 {
- newtmp = append(newtmp, v[0:2], v[2:])
- } else {
- newtmp = append(newtmp, v)
- }
- }
- tmps = newtmp
- }
- timestr := "" //2006-01-02 15:04:05
- timestamp := int64(0)
- if len(tmps) == 1 {
- if len(tmps[0]) == 8 {
- timestr = tmps[0][0:4] + "-" + tmps[0][4:6] + "-" + tmps[0][6:8]
- t, _ := time.ParseInLocation("2006-01-02-15-04", timestr+"-09-00", time.Local)
- timestamp = t.Unix()
- } else if len(tmps[0]) == 14 {
- 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]
- t, _ := time.ParseInLocation("2006-01-02 15:04:00", timestr, time.Local)
- timestamp = t.Unix()
- }
- } else if len(tmps) == 2 {
- timestr = fmt.Sprint(time.Now().Year()) + "-" + MDhmsRepair(tmps[0]) + "-" + MDhmsRepair(tmps[1])
- t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
- timestamp = t.Unix()
- } else if len(tmps) == 3 {
- timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2])
- t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
- timestamp = t.Unix()
- } else if len(tmps) == 4 {
- timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3])
- t, _ := time.ParseInLocation("2006-01-02 15", timestr, time.Local)
- timestamp = t.Unix()
- } else if len(tmps) >= 5 {
- timestr = tmps[0] + "-" + MDhmsRepair(tmps[1]) + "-" + MDhmsRepair(tmps[2]) + " " + MDhmsRepair(tmps[3]) + ":" + MDhmsRepair(tmps[4])
- t, _ := time.ParseInLocation("2006-01-02 15:04", timestr, time.Local)
- timestamp = t.Unix()
- }
- if timestamp <= 0 || timestamp > (time.Now().Unix()+T) {
- data[0] = ""
- } else {
- if addreptime > 0 {
- timestamp += addreptime
- }
- data[0] = timestamp
- }
- return data
- }
- //补位
- func MDhmsRepair(t string) string {
- if len(t) == 1 {
- return "0" + t
- } else {
- return t
- }
- }
- //汉子数和全角转数字
- func chineseToNumber(con string) string {
- tmp := regA.ReplaceAllStringFunc(con, func(key string) string {
- if item[key] != "" {
- return item[key]
- } else {
- return key
- }
- return key
- })
- return tmp
- }
|