From 155f35bd84ec931b05e0ffe90946364f4b2fc98d Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 8 Apr 2020 10:18:51 +0200 Subject: [PATCH] Parse timestamp from message tags in messageLogger.Append --- logger.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/logger.go b/logger.go index fe2a58c..6cfa49b 100644 --- a/logger.go +++ b/logger.go @@ -41,12 +41,21 @@ func (ml *messageLogger) Append(msg *irc.Message) error { return nil } - // TODO: parse time from msg.Tags["time"], if available + var t time.Time + if tag, ok := msg.Tags["time"]; ok { + var err error + t, err = time.Parse(serverTimeLayout, string(tag)) + if err != nil { + return fmt.Errorf("failed to parse message time tag: %v", err) + } + t = t.In(time.Local) + } else { + t = time.Now() + } // TODO: enforce maximum open file handles (LRU cache of file handles) // TODO: handle non-monotonic clock behaviour - now := time.Now() - path := logPath(ml.network, ml.entity, now) + path := logPath(ml.network, ml.entity, t) if ml.path != path { if ml.file != nil { ml.file.Close() @@ -66,7 +75,7 @@ func (ml *messageLogger) Append(msg *irc.Message) error { ml.file = f } - _, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", now.Hour(), now.Minute(), now.Second(), s) + _, err := fmt.Fprintf(ml.file, "[%02d:%02d:%02d] %s\n", t.Hour(), t.Minute(), t.Second(), s) if err != nil { return fmt.Errorf("failed to log message to %q: %v", ml.path, err) }