From 0c11cae782ce3dc83eb1628b545bb83063120858 Mon Sep 17 00:00:00 2001 From: uppfinnarn Date: Thu, 28 Jul 2016 21:14:59 +0200 Subject: [PATCH 1/4] Timestamp type; a string with a Parse() function Closes #204 --- message.go | 4 ++-- structs.go | 24 ++++++++++++------------ types.go | 20 ++++++++++++++++++++ types_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 types.go create mode 100644 types_test.go diff --git a/message.go b/message.go index 8966c16..9e33dd2 100644 --- a/message.go +++ b/message.go @@ -19,8 +19,8 @@ type Message struct { ID string `json:"id"` ChannelID string `json:"channel_id"` Content string `json:"content"` - Timestamp string `json:"timestamp"` - EditedTimestamp string `json:"edited_timestamp"` + Timestamp Timestamp `json:"timestamp"` + EditedTimestamp Timestamp `json:"edited_timestamp"` MentionRoles []string `json:"mention_roles"` Tts bool `json:"tts"` MentionEveryone bool `json:"mention_everyone"` diff --git a/structs.go b/structs.go index 19a291f..2dc7336 100644 --- a/structs.go +++ b/structs.go @@ -137,17 +137,17 @@ type ICEServer struct { // A Invite stores all data related to a specific Discord Guild or Channel invite. type Invite struct { - Guild *Guild `json:"guild"` - Channel *Channel `json:"channel"` - Inviter *User `json:"inviter"` - Code string `json:"code"` - CreatedAt string `json:"created_at"` // TODO make timestamp - MaxAge int `json:"max_age"` - Uses int `json:"uses"` - MaxUses int `json:"max_uses"` - XkcdPass string `json:"xkcdpass"` - Revoked bool `json:"revoked"` - Temporary bool `json:"temporary"` + Guild *Guild `json:"guild"` + Channel *Channel `json:"channel"` + Inviter *User `json:"inviter"` + Code string `json:"code"` + CreatedAt Timestamp `json:"created_at"` + MaxAge int `json:"max_age"` + Uses int `json:"uses"` + MaxUses int `json:"max_uses"` + XkcdPass string `json:"xkcdpass"` + Revoked bool `json:"revoked"` + Temporary bool `json:"temporary"` } // A Channel holds all data related to an individual Discord channel. @@ -204,7 +204,7 @@ type Guild struct { AfkChannelID string `json:"afk_channel_id"` EmbedChannelID string `json:"embed_channel_id"` OwnerID string `json:"owner_id"` - JoinedAt string `json:"joined_at"` // make this a timestamp + JoinedAt Timestamp `json:"joined_at"` Splash string `json:"splash"` AfkTimeout int `json:"afk_timeout"` VerificationLevel VerificationLevel `json:"verification_level"` diff --git a/types.go b/types.go new file mode 100644 index 0000000..b9541bb --- /dev/null +++ b/types.go @@ -0,0 +1,20 @@ +// Discordgo - Discord bindings for Go +// Available at https://github.com/bwmarrin/discordgo + +// Copyright 2015-2016 Bruce Marriner . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains custom types, currently only a timestamp wrapper. + +package discordgo + +import ( + "time" +) + +type Timestamp string + +func (t Timestamp) Parse() (time.Time, error) { + return time.Parse("2006-01-02T15:04:05.000000-07:00", string(t)) +} diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..1d03aa3 --- /dev/null +++ b/types_test.go @@ -0,0 +1,24 @@ +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") + } +} From a20bdc7fd8fc9e94f0dd1059a8ba217438710872 Mon Sep 17 00:00:00 2001 From: uppfinnarn Date: Thu, 28 Jul 2016 22:36:59 +0200 Subject: [PATCH 2/4] Comments --- types.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/types.go b/types.go index b9541bb..c7c6dd3 100644 --- a/types.go +++ b/types.go @@ -13,8 +13,11 @@ import ( "time" ) +// A timestamp, in the format YYYY-MM-DDTHH:MM:SS.MSMSMS-TZ:TZ. type Timestamp string +// Parse parses a timestamp string into a time.Time object. +// The only time this can fail is if you're parsing an invalid timestamp. func (t Timestamp) Parse() (time.Time, error) { return time.Parse("2006-01-02T15:04:05.000000-07:00", string(t)) } From a223abd357aeeb0e76beb3a3a1cc25adc55a6efa Mon Sep 17 00:00:00 2001 From: uppfinnarn Date: Thu, 28 Jul 2016 22:49:51 +0200 Subject: [PATCH 3/4] Changed the comments for go vet --- types.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types.go b/types.go index c7c6dd3..6b140e3 100644 --- a/types.go +++ b/types.go @@ -13,11 +13,11 @@ import ( "time" ) -// A timestamp, in the format YYYY-MM-DDTHH:MM:SS.MSMSMS-TZ:TZ. +// 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 you're parsing an invalid timestamp. +// The only time this can fail is if Discord changes their timestamp format. func (t Timestamp) Parse() (time.Time, error) { return time.Parse("2006-01-02T15:04:05.000000-07:00", string(t)) } From a2e6e1da453ec48e74c3dbf8bbb7e25694210654 Mon Sep 17 00:00:00 2001 From: robbix1206 Date: Sat, 3 Sep 2016 23:38:34 +0200 Subject: [PATCH 4/4] Fix the pattern The Pattern was not the correct one fixing it by the pattern of RFC3339 --- types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types.go b/types.go index 6b140e3..24f52c4 100644 --- a/types.go +++ b/types.go @@ -19,5 +19,5 @@ 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("2006-01-02T15:04:05.000000-07:00", string(t)) + return time.Parse(time.RFC3339, string(t)) }