~taiite/senpai

8123af1481a5d442473f3a695d0ec58c587ac79e — delthas 2 months ago 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 insertions(+), 8 deletions(-)

M irc/tokens.go
M irc/tokens.go => irc/tokens.go +5 -8
@@ 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.