From 1a56b2f658e9038442309b430769167413a665c1 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 16 Feb 2022 14:45:09 +0100 Subject: [PATCH] Introduce formatServerTime It's too easy to forget to convert to UTC. --- bridge.go | 2 +- db_sqlite.go | 2 +- downstream.go | 12 ++++++------ irc.go | 4 ++++ msgstore_fs.go | 2 +- upstream.go | 2 +- 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bridge.go b/bridge.go index dc8ef75..fda02ff 100644 --- a/bridge.go +++ b/bridge.go @@ -27,7 +27,7 @@ func forwardChannel(ctx context.Context, dc *downstreamConn, ch *upstreamChannel } else { timestampStr := "*" if r != nil { - timestampStr = fmt.Sprintf("timestamp=%s", r.Timestamp.UTC().Format(serverTimeLayout)) + timestampStr = fmt.Sprintf("timestamp=%s", formatServerTime(r.Timestamp)) } dc.SendMessage(&irc.Message{ Prefix: dc.prefix(), diff --git a/db_sqlite.go b/db_sqlite.go index 4929b58..44556eb 100644 --- a/db_sqlite.go +++ b/db_sqlite.go @@ -795,7 +795,7 @@ func (db *SqliteDB) StoreReadReceipt(ctx context.Context, networkID int64, recei args := []interface{}{ sql.Named("id", receipt.ID), - sql.Named("timestamp", receipt.Timestamp.UTC().Format(serverTimeLayout)), + sql.Named("timestamp", formatServerTime(receipt.Timestamp)), sql.Named("network", networkID), sql.Named("target", receipt.Target), } diff --git a/downstream.go b/downstream.go index 30ef902..2ee35f9 100644 --- a/downstream.go +++ b/downstream.go @@ -2330,7 +2330,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. dc.logger.Printf("broadcasting bouncer-wide %v: %v", msg.Command, text) broadcastTags := tags.Copy() - broadcastTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout)) + broadcastTags["time"] = irc.TagValue(formatServerTime(time.Now())) broadcastMsg := &irc.Message{ Tags: broadcastTags, Prefix: servicePrefix, @@ -2356,7 +2356,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. if msg.Command == "PRIVMSG" && casemapASCII(name) == serviceNickCM { if dc.caps["echo-message"] { echoTags := tags.Copy() - echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout)) + echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) dc.SendMessage(&irc.Message{ Tags: echoTags, Prefix: dc.prefix(), @@ -2388,7 +2388,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. }) echoTags := tags.Copy() - echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout)) + echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) if uc.account != "" { echoTags["account"] = irc.TagValue(uc.account) } @@ -2439,7 +2439,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. }) echoTags := tags.Copy() - echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout)) + echoTags["time"] = irc.TagValue(formatServerTime(time.Now())) if uc.account != "" { echoTags["account"] = irc.TagValue(uc.account) } @@ -2736,7 +2736,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. Tags: irc.Tags{"batch": batchRef}, Prefix: dc.srv.prefix(), Command: "CHATHISTORY", - Params: []string{"TARGETS", target.Name, target.LatestMessage.UTC().Format(serverTimeLayout)}, + Params: []string{"TARGETS", target.Name, formatServerTime(target.LatestMessage)}, }) } }) @@ -2832,7 +2832,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc. timestampStr := "*" if !r.Timestamp.IsZero() { - timestampStr = fmt.Sprintf("timestamp=%s", r.Timestamp.UTC().Format(serverTimeLayout)) + timestampStr = fmt.Sprintf("timestamp=%s", formatServerTime(r.Timestamp)) } uc.forEachDownstream(func(d *downstreamConn) { if broadcast || dc.id == d.id { diff --git a/irc.go b/irc.go index 407d6aa..ba44048 100644 --- a/irc.go +++ b/irc.go @@ -31,6 +31,10 @@ const ( // The server-time layout, as defined in the IRCv3 spec. const serverTimeLayout = "2006-01-02T15:04:05.000Z" +func formatServerTime(t time.Time) string { + return t.UTC().Format(serverTimeLayout) +} + type userModes string func (ms userModes) Has(c byte) bool { diff --git a/msgstore_fs.go b/msgstore_fs.go index f587371..ebed557 100644 --- a/msgstore_fs.go +++ b/msgstore_fs.go @@ -380,7 +380,7 @@ func parseMessage(line, entity string, ref time.Time, events bool) (*irc.Message msg := &irc.Message{ Tags: map[string]irc.TagValue{ - "time": irc.TagValue(t.UTC().Format(serverTimeLayout)), + "time": irc.TagValue(formatServerTime(t)), }, Prefix: prefix, Command: cmd, diff --git a/upstream.go b/upstream.go index c6a4bb3..97cc960 100644 --- a/upstream.go +++ b/upstream.go @@ -445,7 +445,7 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err } if _, ok := msg.Tags["time"]; !ok { - msg.Tags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout)) + msg.Tags["time"] = irc.TagValue(formatServerTime(time.Now())) } switch msg.Command {