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 } var tt time.Time if len(b) > 10 { tt = time.Unix(0, i) } else { tt = time.Unix(i, 0) } *t = UnixTimestamp(tt) 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`) }