summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordelthas <delthas@dille.cc>2022-04-20 19:24:30 +0200
committerdelthas <delthas@dille.cc>2022-04-20 19:24:30 +0200
commit8123af1481a5d442473f3a695d0ec58c587ac79e (patch)
treee7a3534e17cffe251cdea09303cd068591c58d9c
parentSend SASL PLAIN authentication on connect (diff)
Use time.Parse for parsing incoming server times
Following a pprof profiling, using fmt.Scanf was slow. time.Parse is faster and more readable.
-rw-r--r--irc/tokens.go13
1 files 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.