From 8123af1481a5d442473f3a695d0ec58c587ac79e Mon Sep 17 00:00:00 2001 From: delthas Date: Wed, 20 Apr 2022 19:24:30 +0200 Subject: [PATCH] Use time.Parse for parsing incoming server times Following a pprof profiling, using fmt.Scanf was slow. time.Parse is faster and more readable. --- irc/tokens.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/irc/tokens.go b/irc/tokens.go index 7bcde99..ed63535 100644 --- a/irc/tokens.go +++ b/irc/tokens.go @@ -366,17 +366,14 @@ func (msg *Message) ParseParams(out ...*string) error { return nil } -func parseTimestamp(timestamp string) (time.Time, bool) { - var year, month, day, hour, minute, second, millis int - - timestamp = strings.TrimSuffix(timestamp, "Z") +const serverTimeLayout = "2006-01-02T15:04:05.000Z" - _, err := fmt.Sscanf(timestamp, "%4d-%2d-%2dT%2d:%2d:%2d.%3d", &year, &month, &day, &hour, &minute, &second, &millis) - if err != nil || month < 1 || 12 < month { +func parseTimestamp(timestamp string) (time.Time, bool) { + t, err := time.Parse(serverTimeLayout, timestamp) + if err != nil { return time.Time{}, false } - - return time.Date(year, time.Month(month), day, hour, minute, second, millis*1e6, time.UTC), true + return t.UTC(), true } // Time returns the time when the message has been sent, if present. -- 2.34.2