Created
October 18, 2025 07:21
-
-
Save wjkoh/08d00d62e32f581051b66b19c3af29c5 to your computer and use it in GitHub Desktop.
Go: UnixTime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, ×tamp); 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