fish2018/pansou · warning
无法解析日期时间
Error message
无法解析日期时间: %s
What it means
parseDateTime tried every layout in its layouts slice against dateStr and none matched, so it returns this error with the offending string. The plugin scrapes human-formatted post dates, and any format variation breaks parsing.
Solutions
- Log dateStr and add the missing layout to the layouts slice (time.Parse layouts, reference 2006-01-02 15:04:05).
- Clean the string first: trim whitespace, decode HTML entities (e.g. ), collapse inner spaces.
- Add fallback handling for relative dates (天前/小时前/分钟前) by converting them to absolute times.
- Return a sensible zero-value fallback (e.g. time.Now()) for display-only fields instead of failing the whole detail parse.
Example fix
// before
layouts := []string{"2006-01-02 15:04:05", "2006-01-02"}
for _, layout := range layouts {
if t, err := time.Parse(layout, dateStr); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("无法解析日期时间: %s", dateStr)
// after
dateStr = strings.TrimSpace(dateStr)
if days, ok := matchRelativeDays(dateStr); ok { // e.g. "3天前"
return time.Now().AddDate(0, 0, -days), nil
}
layouts := append(layouts, "2006-01-02 15:04", "2006年01月02日 15:04")
for _, layout := range layouts {
if t, err := time.Parse(layout, dateStr); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("无法解析日期时间: %s", dateStr) Defensive patterns
Strategy: fallback
Validate before calling
dateStr = strings.TrimSpace(html.UnescapeString(rawDate))
if dateStr == "" {
// no date available; skip parse entirely
return
}
for _, layout := range knownLayouts {
if _, err := time.Parse(layout, dateStr); err == nil {
return
}
} Type guard
func looksLikeDate(s string) bool {
s = strings.TrimSpace(s)
for _, layout := range knownLayouts {
if _, err := time.Parse(layout, s); err == nil {
return true
}
}
return false
} Try / catch
t, err := parseDateTime(raw)
if err != nil {
log.Printf("date parse failed for %q, using now: %v", raw, err)
t = time.Now() // degrade gracefully for display-only usage
} Prevention
- Clean scraped text (trim, unescape entities) before parsing dates
- Keep a broad layout list and add new site formats as soon as observed
- Support relative dates (昨天/小时前) with explicit converters
- Treat dates as non-critical: fall back instead of failing the whole item
When it happens
Trigger: A date string extracted from a detail page does not match any of the predefined time.Parse layouts — e.g. locale-specific month names, missing timezone suffix, new format like '3 天前', or extra whitespace/HTML entities left in the string.
Common situations: Site changes its date rendering, relative dates ('昨天', 'x 小时前') appearing instead of absolute timestamps, Chinese-vs-English month names, or uncleaned whitespace/nbsp in the extracted text.
Related errors
AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07).
Data as JSON: /api/errors/f92cf7f115c8cd0f.
Report an issue: GitHub.
Appendix: source
Thrown at plugin/hdr4k/hdr4k.go:641
}
// parseDateTime 解析日期时间字符串
func (p *Hdr4kAsyncPlugin) parseDateTime(dateStr string) (time.Time, error) {
// 4KHDR的时间格式:2025-4-9 19:55
layouts := []string{
"2006-1-2 15:04",
"2006-01-02 15:04:05",
"2006-1-2 15:04:05",
"2006-01-02 15:04",
}
for _, layout := range layouts {
if t, err := time.Parse(layout, dateStr); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("无法解析日期时间: %s", dateStr)
}
// cleanHTML 清理HTML标签和特殊字符
func (p *Hdr4kAsyncPlugin) cleanHTML(html string) string {
// 替换常见HTML标签和实体
replacements := map[string]string{
"<strong>": "",
"</strong>": "",
"<font color=\"#ff0000\">": "",
"</font>": "",
"<em>": "",
"</em>": "",
"<b>": "",
"</b>": "",
"<br>": "\n",
"<br/>": "\n",
"<br />": "\n",
" ": " ",View on GitHub (pinned to beaa561337)