Introduce formatServerTime

It's too easy to forget to convert to UTC.
This commit is contained in:
Simon Ser 2022-02-16 14:45:09 +01:00
parent 155e811cd9
commit 1a56b2f658
6 changed files with 14 additions and 10 deletions

View File

@ -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(),

View File

@ -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),
}

View File

@ -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 {

4
irc.go
View File

@ -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 {

View File

@ -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,

View File

@ -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 {