2020-01-02 05:39:52 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-05-08 21:11:56 +00:00
|
|
|
"strconv"
|
2020-01-17 22:29:13 +00:00
|
|
|
"strings"
|
2020-01-02 05:39:52 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-07-29 20:10:17 +00:00
|
|
|
// Timestamp has a valid zero-value, which can be checked using the IsValid()
|
2020-02-11 04:26:22 +00:00
|
|
|
// method. This is useful for optional timestamps such as EditedTimestamp.
|
2020-01-02 05:39:52 +00:00
|
|
|
type Timestamp time.Time
|
|
|
|
|
|
|
|
const TimestampFormat = time.RFC3339 // same as ISO8601
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ json.Unmarshaler = (*Timestamp)(nil)
|
|
|
|
_ json.Marshaler = (*Timestamp)(nil)
|
|
|
|
)
|
|
|
|
|
2020-01-26 09:06:54 +00:00
|
|
|
func NewTimestamp(t time.Time) Timestamp {
|
|
|
|
return Timestamp(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NowTimestamp() Timestamp {
|
|
|
|
return NewTimestamp(time.Now())
|
|
|
|
}
|
|
|
|
|
2020-04-01 03:45:56 +00:00
|
|
|
// UnmarshalJSON parses a nullable RFC3339 string into time.
|
2020-01-02 05:39:52 +00:00
|
|
|
func (t *Timestamp) UnmarshalJSON(v []byte) error {
|
2020-01-17 22:29:13 +00:00
|
|
|
str := strings.Trim(string(v), `"`)
|
|
|
|
if str == "null" {
|
2021-10-30 23:43:28 +00:00
|
|
|
*t = Timestamp{}
|
2020-01-17 22:29:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := time.Parse(TimestampFormat, str)
|
2020-01-02 05:39:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = Timestamp(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-01 03:45:56 +00:00
|
|
|
// MarshalJSON returns null if Timestamp is not valid (zero). It returns the
|
|
|
|
// time formatted in RFC3339 otherwise.
|
2020-01-02 05:39:52 +00:00
|
|
|
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
2020-07-29 20:10:17 +00:00
|
|
|
if !t.IsValid() {
|
2020-01-19 06:06:00 +00:00
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:06:51 +00:00
|
|
|
return []byte(`"` + t.Format(TimestampFormat) + `"`), nil
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-07-29 20:10:17 +00:00
|
|
|
func (t Timestamp) IsValid() bool {
|
2020-01-21 01:06:51 +00:00
|
|
|
return !t.Time().IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Timestamp) Format(fmt string) string {
|
|
|
|
return t.Time().Format(fmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Timestamp) Time() time.Time {
|
|
|
|
return time.Time(t)
|
2020-01-20 08:53:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
type UnixTimestamp int64
|
|
|
|
|
|
|
|
func (t UnixTimestamp) String() string {
|
|
|
|
return t.Time().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t UnixTimestamp) Time() time.Time {
|
|
|
|
return time.Unix(int64(t), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
type UnixMsTimestamp int64
|
|
|
|
|
2020-03-31 04:46:03 +00:00
|
|
|
func TimeToMilliseconds(t time.Time) UnixMsTimestamp {
|
|
|
|
return UnixMsTimestamp(t.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:43:34 +00:00
|
|
|
func (t UnixMsTimestamp) String() string {
|
|
|
|
return t.Time().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t UnixMsTimestamp) Time() time.Time {
|
|
|
|
return time.Unix(0, int64(t)*int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
type Seconds int
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-08 21:11:56 +00:00
|
|
|
// NullSecond is used in cases where null should be used instead of a number or
|
|
|
|
// omitted. This is similar to NullSnowflake.
|
|
|
|
const NullSecond = -1
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func DurationToSeconds(dura time.Duration) Seconds {
|
|
|
|
return Seconds(dura.Seconds())
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:11:56 +00:00
|
|
|
func (s Seconds) MarshalJSON() ([]byte, error) {
|
|
|
|
if s < 1 {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
2021-08-08 20:19:15 +00:00
|
|
|
|
|
|
|
return []byte(strconv.Itoa(int(s))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Seconds) UnmarshalJSON(data []byte) error {
|
|
|
|
if string(data) == "null" {
|
|
|
|
*s = NullSecond
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(data, (*int)(s))
|
2020-05-08 21:11:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func (s Seconds) String() string {
|
|
|
|
return s.Duration().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Seconds) Duration() time.Duration {
|
|
|
|
return time.Duration(s) * time.Second
|
|
|
|
}
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2021-08-30 15:41:54 +00:00
|
|
|
// OptionalSeconds is the option type for Seconds.
|
|
|
|
type OptionalSeconds = *Seconds
|
|
|
|
|
|
|
|
// ZeroOptionalSeconds are 0 OptionalSeconds.
|
|
|
|
var ZeroOptionalSeconds = NewOptionalSeconds(0)
|
|
|
|
|
|
|
|
// NewOptionalSeconds creates a new OptionalSeconds using the value of the
|
|
|
|
// passed Seconds.
|
|
|
|
func NewOptionalSeconds(s Seconds) OptionalSeconds { return &s }
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Milliseconds is in float64 because some Discord events return time with a
|
|
|
|
// trailing decimal.
|
|
|
|
type Milliseconds float64
|
2020-01-15 04:43:34 +00:00
|
|
|
|
|
|
|
func DurationToMilliseconds(dura time.Duration) Milliseconds {
|
|
|
|
return Milliseconds(dura.Milliseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms Milliseconds) String() string {
|
|
|
|
return ms.Duration().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms Milliseconds) Duration() time.Duration {
|
2020-04-25 02:36:33 +00:00
|
|
|
const f64ms = Milliseconds(time.Millisecond)
|
|
|
|
return time.Duration(ms * f64ms)
|
2020-01-15 04:43:34 +00:00
|
|
|
}
|
2021-08-08 20:19:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// ArchiveDuration is the duration after which a thread without activity will
|
|
|
|
// be archived.
|
|
|
|
//
|
|
|
|
// The duration's unit is minutes.
|
|
|
|
type ArchiveDuration int
|
|
|
|
|
|
|
|
const (
|
|
|
|
OneHourArchive ArchiveDuration = 60
|
|
|
|
OneDayArchive ArchiveDuration = 24 * OneHourArchive
|
|
|
|
// ThreeDaysArchive archives a thread after three days.
|
|
|
|
//
|
|
|
|
// This duration is only available to nitro boosted guilds. The Features
|
|
|
|
// field of a Guild will indicate whether this is the case.
|
|
|
|
ThreeDaysArchive ArchiveDuration = 3 * OneDayArchive
|
|
|
|
// SevenDaysArchive archives a thread after seven days.
|
|
|
|
//
|
|
|
|
// This duration is only available to nitro boosted guilds. The Features
|
|
|
|
// field of a Guild will indicate whether this is the case.
|
|
|
|
SevenDaysArchive ArchiveDuration = 7 * OneDayArchive
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m ArchiveDuration) String() string {
|
|
|
|
return m.Duration().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m ArchiveDuration) Duration() time.Duration {
|
|
|
|
return time.Duration(m) * time.Minute
|
|
|
|
}
|