2018-04-12 09:38:04 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Timestamp time.Time
|
|
|
|
|
|
|
|
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
|
|
|
ts := time.Time(t).Unix()
|
|
|
|
stamp := fmt.Sprint(ts * 1000)
|
|
|
|
return []byte(stamp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Timestamp) UnmarshalJSON(b []byte) error {
|
|
|
|
ts, err := strconv.Atoi(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*t = Timestamp(time.Unix(int64(ts)/1000, 0))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Timestamp) String() string {
|
|
|
|
return time.Time(t).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Now() Timestamp {
|
|
|
|
return Timestamp(time.Now())
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:35:11 +00:00
|
|
|
func NowPtr() *Timestamp {
|
|
|
|
n := Now()
|
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
2018-04-12 09:38:04 +00:00
|
|
|
func Date(year int, month time.Month, day int) Timestamp {
|
|
|
|
return Timestamp(time.Date(year, month, day, 0, 0, 0, 0, time.UTC))
|
|
|
|
}
|