123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package main
- import (
- "fmt"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- var spaces = []string{"\u3000", "\u2003", "\u00a0", "\t", "\r", "\n", "\u0001"}
- var reg, regA, regB, regC, regD,regE,regF, regAfter ,regAfterBool*regexp.Regexp
- const (
- T = 365 * 86400
- )
- var item = map[string]string{
- "一": "1", "二": "2", "三": "3", "四": "4", "五": "5",
- "六": "6", "七": "7", "八": "8","九": "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})时`)
- regE, _ = regexp.Compile(`^([0-9一二三四五六七八九十]+)月(份)?$`)
- regF, _ = regexp.Compile(`^(\d{4})(\d{2})$`)
- regAfter, _ = regexp.Compile(`(下午D?\d{1,2}[时|:|:|h|H])`)
- regAfterBool, _ = regexp.Compile(`(下午D?[1-2][0-9][时|:|:|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 cleanStrToTimestamp(time_str string,publishtime int64) int64 {
- tmp := time_str
- //处理类似:二〇一五年十一月四日十五时
- 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)
- //2021年09月10日下午15时30分
- if regAfter.MatchString(tmp) && !regAfterBool.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()
- if timestamp<=0 {
- timestr = fmt.Sprint(MDhmsRepair(tmps[0]) + "-" + MDhmsRepair(tmps[1])+"-01")
- 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 regE.MatchString(tmp) && timestamp<=0 {
- m := 0
- new_str := regE.ReplaceAllString(tmp,"$1")
- str := chineseToNumber(new_str)
- it, _ := strconv.Atoi(str)
- if it >100 {
- m = 10+it%100
- }else {
- m = it
- }
- if m>0&&m<13 {
- m_s := fmt.Sprintf("%d",m)
- //优先取发布时间-的年份--待完善
- y_s := fmt.Sprintf("%d",time.Unix(publishtime, 0).Year())
- //y_s := fmt.Sprintf("%d",time.Now().Year())
- timestr = y_s + "-" + MDhmsRepair(m_s) + "-01"
- t, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
- if t.Unix()>publishtime {
- timestamp = t.Unix()
- }else {
- y_s = fmt.Sprintf("%d",time.Unix(publishtime, 0).Year()+1)
- timestr = y_s + "-" + MDhmsRepair(m_s) + "-01"
- t1, _ := time.ParseInLocation("2006-01-02", timestr, time.Local)
- timestamp = t1.Unix()
- }
- }
- }
- if regF.MatchString(tmp) && timestamp<=0 {
- new_str := regF.ReplaceAllString(tmp,"$1-$2-01")
- t, _ := time.ParseInLocation("2006-01-02", new_str, time.Local)
- timestamp = t.Unix()
- }
- if timestamp <= 0 || timestamp > (time.Now().Unix()+T) {
- timestamp = 0
- } else {
- if addreptime > 0 {
- timestamp += addreptime
- }
- }
- return timestamp
- }
- //汉子数和全角转数字
- 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
- }
- //补位
- func MDhmsRepair(t string) string {
- if len(t) == 1 {
- return "0" + t
- } else {
- return t
- }
- }
|