Remove Timestamp type, use time.Time

This commit is contained in:
Carson Hoffman 2021-12-24 12:38:50 -05:00
parent 092735083d
commit fccf6db03e
No known key found for this signature in database
GPG key ID: 05B660CB452C657F
5 changed files with 14 additions and 47 deletions

View file

@ -14,6 +14,7 @@ import (
"io"
"regexp"
"strings"
"time"
)
// MessageType is the type of Message
@ -60,11 +61,11 @@ type Message struct {
// CAUTION: this field may be removed in a
// future API version; it is safer to calculate
// the creation time via the ID.
Timestamp Timestamp `json:"timestamp"`
Timestamp time.Time `json:"timestamp"`
// The time at which the last edit of the message
// occurred, if it has been edited.
EditedTimestamp Timestamp `json:"edited_timestamp"`
EditedTimestamp *time.Time `json:"edited_timestamp"`
// The roles mentioned in the message.
MentionRoles []string `json:"mention_roles"`

View file

@ -306,7 +306,7 @@ func (s *State) MemberAdd(member *Member) error {
} else {
// We are about to replace `m` in the state with `member`, but first we need to
// make sure we preserve any fields that the `member` doesn't contain from `m`.
if member.JoinedAt == "" {
if member.JoinedAt.IsZero() {
member.JoinedAt = m.JoinedAt
}
*m = *member
@ -637,7 +637,7 @@ func (s *State) MessageAdd(message *Message) error {
if message.Content != "" {
m.Content = message.Content
}
if message.EditedTimestamp != "" {
if message.EditedTimestamp != nil {
m.EditedTimestamp = message.EditedTimestamp
}
if message.Mentions != nil {
@ -649,7 +649,7 @@ func (s *State) MessageAdd(message *Message) error {
if message.Attachments != nil {
m.Attachments = message.Attachments
}
if message.Timestamp != "" {
if !message.Timestamp.IsZero() {
m.Timestamp = message.Timestamp
}
if message.Author != nil {

View file

@ -150,7 +150,7 @@ type Integration struct {
ExpireGracePeriod int `json:"expire_grace_period"`
User *User `json:"user"`
Account IntegrationAccount `json:"account"`
SyncedAt Timestamp `json:"synced_at"`
SyncedAt time.Time `json:"synced_at"`
}
// ExpireBehavior of Integration
@ -197,7 +197,7 @@ type Invite struct {
Channel *Channel `json:"channel"`
Inviter *User `json:"inviter"`
Code string `json:"code"`
CreatedAt Timestamp `json:"created_at"`
CreatedAt time.Time `json:"created_at"`
MaxAge int `json:"max_age"`
Uses int `json:"uses"`
MaxUses int `json:"max_uses"`
@ -258,8 +258,8 @@ type Channel struct {
LastMessageID string `json:"last_message_id"`
// The timestamp of the last pinned message in the channel.
// Empty if the channel has no pinned messages.
LastPinTimestamp Timestamp `json:"last_pin_timestamp"`
// nil if the channel has no pinned messages.
LastPinTimestamp *time.Time `json:"last_pin_timestamp"`
// Whether the channel is marked as NSFW.
NSFW bool `json:"nsfw"`
@ -453,7 +453,7 @@ type Guild struct {
// The time at which the current user joined the guild.
// This field is only present in GUILD_CREATE events and websocket
// update events, and thus is only present in state-cached guilds.
JoinedAt Timestamp `json:"joined_at"`
JoinedAt time.Time `json:"joined_at"`
// The hash of the guild's discovery splash.
DiscoverySplash string `json:"discovery_splash"`
@ -782,8 +782,8 @@ type Member struct {
// The guild ID on which the member exists.
GuildID string `json:"guild_id"`
// The time at which the member joined the guild, in ISO8601.
JoinedAt Timestamp `json:"joined_at"`
// The time at which the member joined the guild.
JoinedAt time.Time `json:"joined_at"`
// The nickname of the member, if they have one.
Nick string `json:"nick"`
@ -801,7 +801,7 @@ type Member struct {
Roles []string `json:"roles"`
// When the user used their Nitro boost on the server
PremiumSince Timestamp `json:"premium_since"`
PremiumSince *time.Time `json:"premium_since"`
// Is true while the member hasn't accepted the membership screen.
Pending bool `json:"pending"`

View file

@ -12,18 +12,8 @@ package discordgo
import (
"encoding/json"
"net/http"
"time"
)
// Timestamp stores a timestamp, as sent by the Discord API.
type Timestamp string
// Parse parses a timestamp string into a time.Time object.
// The only time this can fail is if Discord changes their timestamp format.
func (t Timestamp) Parse() (time.Time, error) {
return time.Parse(time.RFC3339, string(t))
}
// RESTError stores error information about a request with a bad response code.
// Message is not always present, there are cases where api calls can fail
// without returning a json message.

View file

@ -1,24 +0,0 @@
package discordgo
import (
"testing"
"time"
)
func TestTimestampParse(t *testing.T) {
ts, err := Timestamp("2016-03-24T23:15:59.605000+00:00").Parse()
if err != nil {
t.Fatal(err)
}
if ts.Year() != 2016 || ts.Month() != time.March || ts.Day() != 24 {
t.Error("Incorrect date")
}
if ts.Hour() != 23 || ts.Minute() != 15 || ts.Second() != 59 {
t.Error("Incorrect time")
}
_, offset := ts.Zone()
if offset != 0 {
t.Error("Incorrect timezone")
}
}