diff --git a/rfctime/interface.go b/rfctime/interface.go index 3dc2996..d197158 100644 --- a/rfctime/interface.go +++ b/rfctime/interface.go @@ -43,3 +43,45 @@ type RFCTime interface { GoString() string String() string } + +type RFCDuration interface { + Time() time.Time + Serialize() string + + UnmarshalJSON(bytes []byte) error + 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 + Month() time.Month + Day() int + Weekday() time.Weekday + ISOWeek() (year, week int) + Clock() (hour, min, sec int) + Hour() int + Minute() int + Second() int + Nanosecond() int + YearDay() int + Sub(u RFCTime) time.Duration + Unix() int64 + UnixMilli() int64 + UnixMicro() int64 + UnixNano() int64 + Format(layout string) string + GoString() string + String() string +} diff --git a/rfctime/seconds.go b/rfctime/seconds.go new file mode 100644 index 0000000..e185e19 --- /dev/null +++ b/rfctime/seconds.go @@ -0,0 +1,59 @@ +package rfctime + +import ( + "encoding/json" + "gogs.mikescher.com/BlackForestBytes/goext/timeext" + "time" +) + +type SecondsF64 time.Duration + +func (d SecondsF64) Duration() time.Duration { + return time.Duration(d) +} + +func (d SecondsF64) String() string { + return d.Duration().String() +} + +func (d SecondsF64) Nanoseconds() int64 { + return d.Duration().Nanoseconds() +} + +func (d SecondsF64) Microseconds() int64 { + return d.Duration().Microseconds() +} + +func (d SecondsF64) Milliseconds() int64 { + return d.Duration().Milliseconds() +} + +func (d SecondsF64) Seconds() float64 { + return d.Duration().Seconds() +} + +func (d SecondsF64) Minutes() float64 { + return d.Duration().Minutes() +} + +func (d SecondsF64) Hours() float64 { + return d.Duration().Hours() +} + +func (d *SecondsF64) UnmarshalJSON(data []byte) error { + var secs float64 = 0 + if err := json.Unmarshal(data, &secs); err != nil { + return err + } + *d = SecondsF64(timeext.FromSeconds(secs)) + return nil +} + +func (d SecondsF64) MarshalJSON() ([]byte, error) { + secs := d.Seconds() + return json.Marshal(secs) +} + +func NewSecondsF64(t time.Duration) SecondsF64 { + return SecondsF64(t) +}