commit 8123af1

delthas  ·  2022-04-20 17:24:30 +0000 UTC
parent 0099823
Use time.Parse for parsing incoming server times

Following a pprof profiling, using fmt.Scanf was slow. time.Parse is
faster and more readable.
1 files changed,  +5, -8
+5, -8
 1@@ -366,17 +366,14 @@ func (msg *Message) ParseParams(out ...*string) error {
 2 	return nil
 3 }
 4 
 5-func parseTimestamp(timestamp string) (time.Time, bool) {
 6-	var year, month, day, hour, minute, second, millis int
 7-
 8-	timestamp = strings.TrimSuffix(timestamp, "Z")
 9+const serverTimeLayout = "2006-01-02T15:04:05.000Z"
10 
11-	_, err := fmt.Sscanf(timestamp, "%4d-%2d-%2dT%2d:%2d:%2d.%3d", &year, &month, &day, &hour, &minute, &second, &millis)
12-	if err != nil || month < 1 || 12 < month {
13+func parseTimestamp(timestamp string) (time.Time, bool) {
14+	t, err := time.Parse(serverTimeLayout, timestamp)
15+	if err != nil {
16 		return time.Time{}, false
17 	}
18-
19-	return time.Date(year, time.Month(month), day, hour, minute, second, millis*1e6, time.UTC), true
20+	return t.UTC(), true
21 }
22 
23 // Time returns the time when the message has been sent, if present.