From 7a077baf3769e2a4336c8fd12c3cd512c0a4acb7 Mon Sep 17 00:00:00 2001 From: mileusna Date: Tue, 20 Jun 2017 23:02:07 +0200 Subject: [PATCH] Modify timestamp UnmarshalJSON for accurate time Add documentation --- timestamp.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/timestamp.go b/timestamp.go index 78487cc..0ae48ed 100644 --- a/timestamp.go +++ b/timestamp.go @@ -6,24 +6,24 @@ import ( "time" ) +// Timestamp struct for easily MarshalJSON/UnmarshalJSON unix timestamp to time.Time type Timestamp struct { time.Time } +// MarshalJSON converts golang time to unix timestam number func (t *Timestamp) MarshalJSON() ([]byte, error) { ts := t.Time.Unix() stamp := fmt.Sprint(ts) - return []byte(stamp), nil } +// UnmarshalJSON converts unix timestamp to golang time func (t *Timestamp) UnmarshalJSON(b []byte) error { ts, err := strconv.Atoi(string(b)) if err != nil { return err } - - t.Time = time.Unix(int64(ts), 0) - + t.Time = time.Unix(int64(ts/1000), 0) return nil }