Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Created December 8, 2014 01:56
Show Gist options
  • Save akhenakh/ece34e80bea4740a05a7 to your computer and use it in GitHub Desktop.
Save akhenakh/ece34e80bea4740a05a7 to your computer and use it in GitHub Desktop.
time.Time timestamp JSON in Go
type JSONTime time.Time
func (t JSONTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%d\"", time.Time(t).Unix())
return []byte(stamp), nil
}
// If you are using gob you need to redefine MarshalBinary
func (t JSONTime) MarshalBinary() ([]byte, error) {
return time.Time(t).MarshalBinary()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment