From 353aeb03894309cc0593731154115886de655a92 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 12 May 2019 03:12:50 -0400 Subject: [PATCH] normalize most times in the app to UTC Fixes #480 --- irc/channel.go | 8 ++++---- irc/client.go | 11 +++++------ irc/connection_limits/throttler.go | 2 +- irc/dline.go | 4 ++-- irc/getters.go | 2 +- irc/handlers.go | 2 +- irc/hostserv.go | 2 +- irc/kline.go | 2 +- irc/server.go | 2 +- 9 files changed, 17 insertions(+), 18 deletions(-) diff --git a/irc/channel.go b/irc/channel.go index 550973af..73ad7806 100644 --- a/irc/channel.go +++ b/irc/channel.go @@ -56,7 +56,7 @@ func NewChannel(s *Server, name string, registered bool) *Channel { } channel := &Channel{ - createdTime: time.Now(), // may be overwritten by applyRegInfo + createdTime: time.Now().UTC(), // may be overwritten by applyRegInfo lists: map[modes.Mode]*UserMaskSet{ modes.BanMask: NewUserMaskSet(), modes.ExceptMask: NewUserMaskSet(), @@ -292,7 +292,7 @@ func (channel *Channel) SetRegistered(founder string) error { return errChannelAlreadyRegistered } channel.registeredFounder = founder - channel.registeredTime = time.Now() + channel.registeredTime = time.Now().UTC() channel.accountToUMode[founder] = modes.ChannelFounder return nil } @@ -686,7 +686,7 @@ func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer) // 2. Send JOIN and MODE lines to channel participants (including the new client) // 3. Replay missed message history to the client func (channel *Channel) Resume(newClient, oldClient *Client, timestamp time.Time) { - now := time.Now() + now := time.Now().UTC() channel.resumeAndAnnounce(newClient, oldClient) if !timestamp.IsZero() { channel.replayHistoryForResume(newClient, timestamp, now) @@ -910,7 +910,7 @@ func (channel *Channel) SetTopic(client *Client, topic string, rb *ResponseBuffe channel.stateMutex.Lock() channel.topic = topic channel.topicSetBy = client.nickMaskString - channel.topicSetTime = time.Now() + channel.topicSetTime = time.Now().UTC() channel.stateMutex.Unlock() prefix := client.NickMaskString() diff --git a/irc/client.go b/irc/client.go index 48857b8b..45705f06 100644 --- a/irc/client.go +++ b/irc/client.go @@ -165,7 +165,7 @@ type ClientDetails struct { // NewClient sets up a new client and runs its goroutine. func RunNewClient(server *Server, conn clientConn) { - now := time.Now() + now := time.Now().UTC() config := server.Config() fullLineLenLimit := ircmsg.MaxlenTagsFromClient + config.Limits.LineLen.Rest // give them 1k of grace over the limit: @@ -409,8 +409,7 @@ func (client *Client) playReattachMessages(session *Session) { // Active updates when the client was last 'active' (i.e. the user should be sitting in front of their client). func (client *Client) Active(session *Session) { - // TODO normalize all times to utc? - now := time.Now() + now := time.Now().UTC() client.stateMutex.Lock() defer client.stateMutex.Unlock() session.atime = now @@ -472,7 +471,7 @@ func (client *Client) tryResume() (success bool) { success = true // this is a bit racey - client.resumeDetails.ResumedAt = time.Now() + client.resumeDetails.ResumedAt = time.Now().UTC() client.nickTimer.Touch(nil) @@ -496,7 +495,7 @@ func (client *Client) tryResume() (success bool) { hostname := client.Hostname() friends := make(ClientSet) - oldestLostMessage := time.Now() + oldestLostMessage := time.Now().UTC() // work out how much time, if any, is not covered by history buffers for _, channel := range channels { @@ -569,7 +568,7 @@ func (client *Client) tryResumeChannels() { // replay direct PRIVSMG history if !details.Timestamp.IsZero() { - now := time.Now() + now := time.Now().UTC() items, complete := client.history.Between(details.Timestamp, now, false, 0) rb := NewResponseBuffer(client.Sessions()[0]) client.replayPrivmsgHistory(rb, items, complete) diff --git a/irc/connection_limits/throttler.go b/irc/connection_limits/throttler.go index e45aabc4..19f589c2 100644 --- a/irc/connection_limits/throttler.go +++ b/irc/connection_limits/throttler.go @@ -45,7 +45,7 @@ type GenericThrottle struct { // it either denies it (by returning false) or allows it (by returning true) // and records it func (g *GenericThrottle) Touch() (throttled bool, remainingTime time.Duration) { - return g.touch(time.Now()) + return g.touch(time.Now().UTC()) } func (g *GenericThrottle) touch(now time.Time) (throttled bool, remainingTime time.Duration) { diff --git a/irc/dline.go b/irc/dline.go index c723c204..44adf28f 100644 --- a/irc/dline.go +++ b/irc/dline.go @@ -34,7 +34,7 @@ type IPBanInfo struct { } func (info IPBanInfo) timeLeft() time.Duration { - return info.TimeCreated.Add(info.Duration).Sub(time.Now()) + return time.Until(info.TimeCreated.Add(info.Duration)) } func (info IPBanInfo) TimeLeft() string { @@ -114,7 +114,7 @@ func (dm *DLineManager) AddNetwork(network net.IPNet, duration time.Duration, re Reason: reason, OperReason: operReason, OperName: operName, - TimeCreated: time.Now(), + TimeCreated: time.Now().UTC(), Duration: duration, } diff --git a/irc/getters.go b/irc/getters.go index cdeadea5..d231d66a 100644 --- a/irc/getters.go +++ b/irc/getters.go @@ -356,7 +356,7 @@ func (channel *Channel) Rename(name, nameCasefolded string) { channel.name = name channel.nameCasefolded = nameCasefolded if channel.registeredFounder != "" { - channel.registeredTime = time.Now() + channel.registeredTime = time.Now().UTC() } channel.stateMutex.Unlock() } diff --git a/irc/handlers.go b/irc/handlers.go index feda4447..6b9a4f45 100644 --- a/irc/handlers.go +++ b/irc/handlers.go @@ -2377,7 +2377,7 @@ func setnameHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *R // TIME func timeHandler(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool { - rb.Add(nil, server.name, RPL_TIME, client.nick, server.name, time.Now().Format(time.RFC1123)) + rb.Add(nil, server.name, RPL_TIME, client.nick, server.name, time.Now().UTC().Format(time.RFC1123)) return false } diff --git a/irc/hostserv.go b/irc/hostserv.go index ff35a80e..a0d7d49c 100644 --- a/irc/hostserv.go +++ b/irc/hostserv.go @@ -176,7 +176,7 @@ func hsRequestHandler(server *Server, client *Client, command string, params []s hsNotice(rb, client.t("An error occurred")) return } - elapsed := time.Now().Sub(account.VHost.LastRequestTime) + elapsed := time.Since(account.VHost.LastRequestTime) remainingTime := server.AccountConfig().VHosts.UserRequests.Cooldown - elapsed // you can update your existing request, but if you were rejected, // you can't spam a replacement request diff --git a/irc/kline.go b/irc/kline.go index 29ee7b79..4099f482 100644 --- a/irc/kline.go +++ b/irc/kline.go @@ -72,7 +72,7 @@ func (km *KLineManager) AddMask(mask string, duration time.Duration, reason, ope Reason: reason, OperReason: operReason, OperName: operName, - TimeCreated: time.Now(), + TimeCreated: time.Now().UTC(), Duration: duration, } km.addMaskInternal(mask, info) diff --git a/irc/server.go b/irc/server.go index 87ced2b0..6944973a 100644 --- a/irc/server.go +++ b/irc/server.go @@ -110,6 +110,7 @@ type clientConn struct { func NewServer(config *Config, logger *logger.Manager) (*Server, error) { // initialize data structures server := &Server{ + ctime: time.Now().UTC(), connectionLimiter: connection_limits.NewLimiter(), connectionThrottler: connection_limits.NewThrottler(), listeners: make(map[string]*ListenerWrapper), @@ -576,7 +577,6 @@ func (server *Server) rehash() error { func (server *Server) applyConfig(config *Config, initial bool) (err error) { if initial { - server.ctime = time.Now() server.configFilename = config.Filename server.name = config.Server.Name server.nameCasefolded = config.Server.nameCasefolded