Skip to content

Instantly share code, notes, and snippets.

@uurtech
Created January 14, 2020 13:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uurtech/a3c1489c88adf53abb558e905bf1cd08 to your computer and use it in GitHub Desktop.
Save uurtech/a3c1489c88adf53abb558e905bf1cd08 to your computer and use it in GitHub Desktop.
JSON unmarshalJson Timestamp to ISO Date
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type CustomTime struct {
Time time.Time
}
const ctLayout = "2006-01-02T15:04:05Z0700"
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
s := string(b)
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
ct.Time = tm
return nil
}
type User struct {
Time CustomTime `json:"Time"`
}
func main() {
var b = User{}
var data = `{"Time":1579008351}`
json.Unmarshal([]byte(data), &b)
fmt.Printf("%v", b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment