Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Created October 18, 2025 07:21
Show Gist options
  • Select an option

  • Save wjkoh/08d00d62e32f581051b66b19c3af29c5 to your computer and use it in GitHub Desktop.

Select an option

Save wjkoh/08d00d62e32f581051b66b19c3af29c5 to your computer and use it in GitHub Desktop.
Go: UnixTime
type UnixTime struct{ time.Time }
func (t *UnixTime) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
t.Time = time.Time{}
return nil
}
var timestamp float64
if err := json.Unmarshal(b, &timestamp); err != nil {
return err
}
seconds, subseconds := math.Modf(timestamp)
nanoseconds := int64(subseconds * 1e9)
t.Time = time.Unix(int64(seconds), nanoseconds)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment