Skip to content

Commit

Permalink
Modify timestamp UnmarshalJSON for accurate time
Browse files Browse the repository at this point in the history
Add documentation
  • Loading branch information
mileusna committed Jun 20, 2017
1 parent b04b4d7 commit 7a077ba
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7a077ba

Please sign in to comment.