This commit is contained in:
Mike Schwörer 2023-03-15 15:41:55 +01:00
parent 86c01659d7
commit a6252f0743
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
7 changed files with 136 additions and 52 deletions

View File

@ -3,24 +3,27 @@ package rfctime
import "time"
type RFCTime interface {
AnyTime
Time() time.Time
Serialize() string
UnmarshalJSON(bytes []byte) error
After(u AnyTime) bool
Before(u AnyTime) bool
Equal(u AnyTime) bool
Sub(u AnyTime) time.Duration
}
type AnyTime interface {
MarshalJSON() ([]byte, error)
MarshalBinary() ([]byte, error)
UnmarshalBinary(data []byte) error
GobEncode() ([]byte, error)
GobDecode(data []byte) error
MarshalText() ([]byte, error)
UnmarshalText(data []byte) error
After(u RFCTime) bool
Before(u RFCTime) bool
Equal(u RFCTime) bool
IsZero() bool
Date() (year int, month time.Month, day int)
Year() int
@ -34,7 +37,6 @@ type RFCTime interface {
Second() int
Nanosecond() int
YearDay() int
Sub(u RFCTime) time.Duration
Unix() int64
UnixMilli() int64
UnixMicro() int64
@ -42,6 +44,8 @@ type RFCTime interface {
Format(layout string) string
GoString() string
String() string
Location() *time.Location
}
type RFCDuration interface {
@ -60,9 +64,9 @@ type RFCDuration interface {
MarshalText() ([]byte, error)
UnmarshalText(data []byte) error
After(u RFCTime) bool
Before(u RFCTime) bool
Equal(u RFCTime) bool
After(u AnyTime) bool
Before(u AnyTime) bool
Equal(u AnyTime) bool
IsZero() bool
Date() (year int, month time.Month, day int)
Year() int
@ -76,7 +80,7 @@ type RFCDuration interface {
Second() int
Nanosecond() int
YearDay() int
Sub(u RFCTime) time.Duration
Sub(u AnyTime) time.Duration
Unix() int64
UnixMilli() int64
UnixMicro() int64
@ -85,3 +89,13 @@ type RFCDuration interface {
GoString() string
String() string
}
func tt(v AnyTime) time.Time {
if r, ok := v.(time.Time); ok {
return r
}
if r, ok := v.(RFCTime); ok {
return r.Time()
}
return time.Unix(0, v.UnixNano()).In(v.Location())
}

50
rfctime/interface_test.go Normal file
View File

@ -0,0 +1,50 @@
package rfctime
import (
"testing"
"time"
)
func TestAnyTimeInterface(t *testing.T) {
var v AnyTime
v = NowRFC3339Nano()
assertEqual(t, v.String(), v.String())
v = NowRFC3339()
assertEqual(t, v.String(), v.String())
v = NowUnix()
assertEqual(t, v.String(), v.String())
v = NowUnixMilli()
assertEqual(t, v.String(), v.String())
v = NowUnixNano()
assertEqual(t, v.String(), v.String())
v = time.Now()
assertEqual(t, v.String(), v.String())
}
func TestRFCTimeInterface(t *testing.T) {
var v RFCTime
v = NowRFC3339Nano()
assertEqual(t, v.String(), v.String())
v = NowRFC3339()
assertEqual(t, v.String(), v.String())
v = NowUnix()
assertEqual(t, v.String(), v.String())
v = NowUnixMilli()
assertEqual(t, v.String(), v.String())
v = NowUnixNano()
assertEqual(t, v.String(), v.String())
}

View File

@ -90,16 +90,16 @@ func (t RFC3339Time) FormatStr() string {
return time.RFC3339
}
func (t RFC3339Time) After(u RFCTime) bool {
return t.Time().After(u.Time())
func (t RFC3339Time) After(u AnyTime) bool {
return t.Time().After(tt(u))
}
func (t RFC3339Time) Before(u RFCTime) bool {
return t.Time().Before(u.Time())
func (t RFC3339Time) Before(u AnyTime) bool {
return t.Time().Before(tt(u))
}
func (t RFC3339Time) Equal(u RFCTime) bool {
return t.Time().Equal(u.Time())
func (t RFC3339Time) Equal(u AnyTime) bool {
return t.Time().Equal(tt(u))
}
func (t RFC3339Time) IsZero() bool {
@ -158,8 +158,8 @@ func (t RFC3339Time) Add(d time.Duration) RFC3339Time {
return RFC3339Time(t.Time().Add(d))
}
func (t RFC3339Time) Sub(u RFCTime) time.Duration {
return t.Time().Sub(u.Time())
func (t RFC3339Time) Sub(u AnyTime) time.Duration {
return t.Time().Sub(tt(u))
}
func (t RFC3339Time) AddDate(years int, months int, days int) RFC3339Time {
@ -194,6 +194,10 @@ func (t RFC3339Time) String() string {
return t.Time().String()
}
func (t RFC3339Time) Location() *time.Location {
return t.Time().Location()
}
func NewRFC3339(t time.Time) RFC3339Time {
return RFC3339Time(t)
}

View File

@ -90,16 +90,16 @@ func (t RFC3339NanoTime) FormatStr() string {
return time.RFC3339Nano
}
func (t RFC3339NanoTime) After(u RFCTime) bool {
return t.Time().After(u.Time())
func (t RFC3339NanoTime) After(u AnyTime) bool {
return t.Time().After(tt(u))
}
func (t RFC3339NanoTime) Before(u RFCTime) bool {
return t.Time().Before(u.Time())
func (t RFC3339NanoTime) Before(u AnyTime) bool {
return t.Time().Before(tt(u))
}
func (t RFC3339NanoTime) Equal(u RFCTime) bool {
return t.Time().Equal(u.Time())
func (t RFC3339NanoTime) Equal(u AnyTime) bool {
return t.Time().Equal(tt(u))
}
func (t RFC3339NanoTime) IsZero() bool {
@ -158,8 +158,8 @@ func (t RFC3339NanoTime) Add(d time.Duration) RFC3339NanoTime {
return RFC3339NanoTime(t.Time().Add(d))
}
func (t RFC3339NanoTime) Sub(u RFCTime) time.Duration {
return t.Time().Sub(u.Time())
func (t RFC3339NanoTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(tt(u))
}
func (t RFC3339NanoTime) AddDate(years int, months int, days int) RFC3339NanoTime {
@ -194,6 +194,10 @@ func (t RFC3339NanoTime) String() string {
return t.Time().String()
}
func (t RFC3339NanoTime) Location() *time.Location {
return t.Time().Location()
}
func NewRFC3339Nano(t time.Time) RFC3339NanoTime {
return RFC3339NanoTime(t)
}

View File

@ -63,16 +63,16 @@ func (t UnixTime) Serialize() string {
return strconv.FormatInt(t.Time().Unix(), 10)
}
func (t UnixTime) After(u RFCTime) bool {
return t.Time().After(u.Time())
func (t UnixTime) After(u AnyTime) bool {
return t.Time().After(tt(u))
}
func (t UnixTime) Before(u RFCTime) bool {
return t.Time().Before(u.Time())
func (t UnixTime) Before(u AnyTime) bool {
return t.Time().Before(tt(u))
}
func (t UnixTime) Equal(u RFCTime) bool {
return t.Time().Equal(u.Time())
func (t UnixTime) Equal(u AnyTime) bool {
return t.Time().Equal(tt(u))
}
func (t UnixTime) IsZero() bool {
@ -131,8 +131,8 @@ func (t UnixTime) Add(d time.Duration) UnixTime {
return UnixTime(t.Time().Add(d))
}
func (t UnixTime) Sub(u RFCTime) time.Duration {
return t.Time().Sub(u.Time())
func (t UnixTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(tt(u))
}
func (t UnixTime) AddDate(years int, months int, days int) UnixTime {
@ -167,6 +167,10 @@ func (t UnixTime) String() string {
return t.Time().String()
}
func (t UnixTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnix(t time.Time) UnixTime {
return UnixTime(t)
}

View File

@ -63,16 +63,16 @@ func (t UnixMilliTime) Serialize() string {
return strconv.FormatInt(t.Time().UnixMilli(), 10)
}
func (t UnixMilliTime) After(u RFCTime) bool {
return t.Time().After(u.Time())
func (t UnixMilliTime) After(u AnyTime) bool {
return t.Time().After(tt(u))
}
func (t UnixMilliTime) Before(u RFCTime) bool {
return t.Time().Before(u.Time())
func (t UnixMilliTime) Before(u AnyTime) bool {
return t.Time().Before(tt(u))
}
func (t UnixMilliTime) Equal(u RFCTime) bool {
return t.Time().Equal(u.Time())
func (t UnixMilliTime) Equal(u AnyTime) bool {
return t.Time().Equal(tt(u))
}
func (t UnixMilliTime) IsZero() bool {
@ -131,8 +131,8 @@ func (t UnixMilliTime) Add(d time.Duration) UnixMilliTime {
return UnixMilliTime(t.Time().Add(d))
}
func (t UnixMilliTime) Sub(u RFCTime) time.Duration {
return t.Time().Sub(u.Time())
func (t UnixMilliTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(tt(u))
}
func (t UnixMilliTime) AddDate(years int, months int, days int) UnixMilliTime {
@ -167,6 +167,10 @@ func (t UnixMilliTime) String() string {
return t.Time().String()
}
func (t UnixMilliTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnixMilli(t time.Time) UnixMilliTime {
return UnixMilliTime(t)
}

View File

@ -63,16 +63,16 @@ func (t UnixNanoTime) Serialize() string {
return strconv.FormatInt(t.Time().UnixNano(), 10)
}
func (t UnixNanoTime) After(u RFCTime) bool {
return t.Time().After(u.Time())
func (t UnixNanoTime) After(u AnyTime) bool {
return t.Time().After(tt(u))
}
func (t UnixNanoTime) Before(u RFCTime) bool {
return t.Time().Before(u.Time())
func (t UnixNanoTime) Before(u AnyTime) bool {
return t.Time().Before(tt(u))
}
func (t UnixNanoTime) Equal(u RFCTime) bool {
return t.Time().Equal(u.Time())
func (t UnixNanoTime) Equal(u AnyTime) bool {
return t.Time().Equal(tt(u))
}
func (t UnixNanoTime) IsZero() bool {
@ -131,8 +131,8 @@ func (t UnixNanoTime) Add(d time.Duration) UnixNanoTime {
return UnixNanoTime(t.Time().Add(d))
}
func (t UnixNanoTime) Sub(u RFCTime) time.Duration {
return t.Time().Sub(u.Time())
func (t UnixNanoTime) Sub(u AnyTime) time.Duration {
return t.Time().Sub(tt(u))
}
func (t UnixNanoTime) AddDate(years int, months int, days int) UnixNanoTime {
@ -167,6 +167,10 @@ func (t UnixNanoTime) String() string {
return t.Time().String()
}
func (t UnixNanoTime) Location() *time.Location {
return t.Time().Location()
}
func NewUnixNano(t time.Time) UnixNanoTime {
return UnixNanoTime(t)
}