glance/internal/glance/unixtime.go
2025-03-30 03:23:02 +02:00

42 lines
1,023 B
Go

package glance
import (
"strconv"
"time"
)
// UnixTime defines a timestamp encoded as epoch seconds in JSON
type UnixTime time.Time
// MarshalJSON is used to convert the timestamp to JSON
func (t UnixTime) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(time.Time(t).Unix(), 10)), nil
}
// UnmarshalJSON is used to convert the timestamp from JSON
func (t *UnixTime) UnmarshalJSON(s []byte) (err error) {
r := string(s)
q, err := strconv.ParseInt(r, 10, 64)
if err != nil {
return err
}
*(*time.Time)(t) = time.Unix(q, 0)
return nil
}
// Unix returns t as a Unix time, the number of seconds elapsed
// since January 1, 1970 UTC. The result does not depend on the
// location associated with t.
func (t UnixTime) Unix() int64 {
return time.Time(t).Unix()
}
// Time returns the JSON time as a time.Time instance in UTC
func (t UnixTime) Time() time.Time {
return time.Time(t).UTC()
}
// String returns t as a formatted string
func (t UnixTime) String() string {
return t.Time().String()
}