2018-06-19 15:20:02 +02:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnixTimestamp is a time.Time that can parse unix timestamps
|
|
|
|
type UnixTimestamp time.Time
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler
|
|
|
|
func (t *UnixTimestamp) UnmarshalText(b []byte) error {
|
|
|
|
i, err := strconv.ParseInt(string(b), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-28 13:54:28 +02:00
|
|
|
|
|
|
|
var tt time.Time
|
|
|
|
if len(b) > 10 {
|
|
|
|
tt = time.Unix(0, i)
|
|
|
|
} else {
|
|
|
|
tt = time.Unix(i, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
*t = UnixTimestamp(tt)
|
2018-06-19 15:20:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler
|
|
|
|
func (t *UnixTimestamp) UnmarshalJSON(b []byte) error {
|
|
|
|
return t.UnmarshalText(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t UnixTimestamp) String() string {
|
|
|
|
return time.Time(t).Format(`2006-01-02 15:04`)
|
|
|
|
}
|