newfetch/tools/time.go

55 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-07-26 09:21:54 +00:00
package tools
import (
"github/fthvgb1/newsfetch/static"
"log"
"strings"
"time"
)
func IsInToday(t string) bool {
if t == "" {
return true
}
t = strings.Trim(t, " ")
t = strings.Trim(t, "\xc2\xa0\xc2\xa0")
t = strings.Replace(t, "\n", "", -1)
t = strings.Replace(t, "\t", "", -1)
if strings.Contains(t, "小时前") {
return true
}
if strings.Contains(t, "天前") {
return false
}
today, _ := time.ParseInLocation(static.TIMEDATE, time.Now().Format(static.TIMEDATE), time.Local)
if len(strings.Fields(t)) > 1 {
nz, err := time.ParseInLocation(static.TIMESTAMPFORMAT, t, time.Local)
if err != nil {
nz, err = time.ParseInLocation(static.TIMESTAMPMinFORMAT, t, time.Local)
}
if err != nil {
log.Printf("can't parse time err[%s]", err)
return true
}
if today.Unix() > nz.Unix() {
return false
}
} else {
nz, err := time.ParseInLocation(static.TIMEDATE, t, time.Local)
if err != nil {
nz, err = time.ParseInLocation(static.TIMEDATEC, t, time.Local)
if err != nil {
nz, err = time.ParseInLocation(static.TIMEDATECC, t, time.Local)
}
}
if err != nil {
log.Printf("can't parse time err[%s]", err)
return true
}
if today.Unix() > nz.Unix() {
return false
}
}
return true
}