Files
logpile/internal/parser/timezone.go
T
Mikhail ChusavitinandClaude Opus 5.5 f2ad0aca96 fix: never use parse time as event or collection time
Dump data is always historical. Parsers stamped undated events with
time.Now() (Inspur component.log memory/fan status, IDL/H3C SEL with
unparsable time, NVIDIA summary/bug-report, generic, XigmaNAS, Unraid,
Redfish replay warnings), so an August dump opened in September showed
DIMM warnings dated the day of analysis.

- Events without a source time keep a zero timestamp.
- Yearless syslog timestamps take the year from the collection time via
  parser.ParseYearlessInDefaultArchiveLocation; zero when unknown.
- Archive/uploaded-snapshot collected_at is no longer defaulted to now;
  only live API collection uses the current time.

ADL-070.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 13:00:53 +03:00

53 lines
1.5 KiB
Go

package parser
import (
"fmt"
"sync"
"time"
)
const fallbackTimezoneName = "Europe/Moscow"
var (
fallbackTimezoneOnce sync.Once
fallbackTimezone *time.Location
)
// DefaultArchiveLocation returns the timezone used for source timestamps
// that do not contain an explicit offset.
func DefaultArchiveLocation() *time.Location {
fallbackTimezoneOnce.Do(func() {
loc, err := time.LoadLocation(fallbackTimezoneName)
if err != nil {
fallbackTimezone = time.FixedZone("MSK", 3*60*60)
return
}
fallbackTimezone = loc
})
return fallbackTimezone
}
// ParseInDefaultArchiveLocation parses timestamps without timezone information
// using Europe/Moscow as the assumed source timezone.
func ParseInDefaultArchiveLocation(layout, value string) (time.Time, error) {
return time.ParseInLocation(layout, value, DefaultArchiveLocation())
}
// ParseYearlessInDefaultArchiveLocation parses a syslog-style timestamp that
// carries no year (e.g. "Jan 2 15:04:05"). The year is taken from ref, the dump
// collection time, and rolled back one year when the result would land after
// ref. Returns zero when ref is unknown: parse time must never supply the year.
func ParseYearlessInDefaultArchiveLocation(layout, value string, ref time.Time) time.Time {
if ref.IsZero() {
return time.Time{}
}
ts, err := ParseInDefaultArchiveLocation(layout+" 2006", fmt.Sprintf("%s %d", value, ref.Year()))
if err != nil {
return time.Time{}
}
if ts.After(ref.Add(48 * time.Hour)) {
ts = ts.AddDate(-1, 0, 0)
}
return ts
}